Socket join and socket.rooms not working #4575
-
Server Side
messageHandler
Client useEffect(() => {
} So when I send message, I print socket.rooms and got these | (iteration index) │ Values │ which means user still didn't join any rooms(?) , and when I emit, it send "HI" to everyone including sender props.chatroomid = chatroom name that I want to use (in number) And every function is ran, as console.log works just fine Any help would be appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
Hey, I was facing the same issue and I solved it by manually setting the clientId and persisting it. here's what I did On the client side,
here socketInUse is the link of my backend and I am setting the customId manually. On the server side, I had to add a middleware with the following code.
So now every request you make to the socket server, you are passing the clientId as the params, and clientId should be the room name which you set in the client. In my case, the clientId was the a variable called roomName, which was stored in the client state. now, when you have added the above code onto the client and the backend, you can emit to your clients from the backend like this
|
Beta Was this translation helpful? Give feedback.
Hey,
so when you use react with socketio, the problem is that every time the app re-renders, the clientID changes, so even if you are making the client join a room, when you emit the message to the room, your client has already updated and is now existing with a different id. So when messages are emitted, it does not reflect on the client side as the client is not really in the room.
I was facing the same issue and I solved it by manually setting the clientId and persisting it.
here's what I did
On the client side,
here socketInUse is the link of my backend and I am setting th…