Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MahdiKarimipour/f8b388086ea08c06e197e585ae1050d5 to your computer and use it in GitHub Desktop.
Save MahdiKarimipour/f8b388086ea08c06e197e585ae1050d5 to your computer and use it in GitHub Desktop.
Pellerex Realtime Messaging Connect to Hub
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