[Vuejs]-Socket.io not sending events to others in room

3👍

First off, socket.to(roomId).emit(...) will send messages to every socket in the roomId EXCEPT for the client represented by socket. If you want to send to all sockets in roomId, you would use io.to(roomId).emit(...). It wasn’t clear to me if you wanted to send to ALL sockets or all sockets except the originator so I want to make sure you understand that aspect of things.

Things to assure yourself with debugging and logging that may shed some light on the issue:

  1. Ensure that both clients connect to your server and stay connected. Logging the actual socket.id on both the connect and disconnect events will give you good picture of that.

  2. Ensure that both clients are requesting the same roomId. If the roomId was different for each, then there would only ever be one socket in the roomId and socket.to().emit() would have nobody else to send to.

  3. Ensure that the client sending the newValue message is not posting a form which then causes the page to reload and thus the old socket is disconnected and a new one created. The logging in point #1 should make sure you don’t see this happening. I mention this mostly because this is a common mistake, though I don’t see enough of your actual HTML page to know if this could be an issue for you or not.

  4. Log every incoming message on your server. Log, not only the message, but also the socket.id that it arrives from. This will allow you to reconstruct the whole message flow.

Without all this data, we don’t have any ability to pinpoint the problem. This type of problem requires gathering data, then following leads until you can see more precisely what is and isn’t happening. If I had to hazard a guess, I’d wonder if you have a problem with either #1 or #2.

Leave a comment