Reputation: 21
I am working on a live chat feature where users can join many different chatrooms. I'm having difficulty subscribing to the new chatroom once switching rooms. It is sending the messages to the correct session, but not receiving them since it's not re-subscripting to the new session.
useEffect(() => {
client.configure({
brokerURL: 'ws://localhost:8080/ws',
connectHeaders: sh,
onConnect: () => {
console.log('onConnect');
setIsConnected(true);
client.subscribe(`/secure/room/${convoId}`, mes => {
const json = JSON.parse(mes.body);
setMessages(prev => prev.concat(json));
});
},
onDisconnect: () => {
console.log('disconnected');
setIsConnected(false);
},
onWebSocketClose: () => {
client.unsubscribe();
}
});
client.activate();
}, [messages, convoId]);
Once a user switches to a new chatroom, to re-subscribe to the session
Upvotes: 0
Views: 141
Reputation: 844
Try to close the connection first before reconfigure it.
like client.disconnect();
Upvotes: 1