Created
December 23, 2021 06:50
-
-
Save MahdiKarimipour/f8b388086ea08c06e197e585ae1050d5 to your computer and use it in GitHub Desktop.
Pellerex Realtime Messaging Connect to Hub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
initiateChatRoomSetup = async () => { | |
const connection = new HubConnectionBuilder() | |
.withUrl( | |
`${'backend-api/hubs/chat'}`, | |
{ | |
accessTokenFactory: () => this.props.accessToken, | |
UseDefaultCredentials: true | |
} | |
) | |
.configureLogging(LogLevel.Trace) | |
.withAutomaticReconnect() | |
.build(); | |
connection.logging = true; | |
connection.start() | |
.then(async result => { | |
//Join Room on Start | |
await connection.invoke("JoinRoom", { | |
userId: this.props.user.userId, | |
chatGroupId: this.state.chatGroupId | |
}); | |
//Events: Receive Message | |
connection.on('ReceiveMessage', this.receiveMessage); | |
//Events: On Close | |
connection.onclose((e) => { }); | |
}) | |
.catch(e => logService.error(`SignalR Chat Connection failed: ${JSON.stringify(e)}`)); | |
this.setState({ | |
...this.state, | |
connection | |
}) | |
}; | |
receiveMessage = (message) => { | |
this.setState({ | |
...this.state, | |
chats: [...this.state.chats ?? [], message] | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment