Wednesday, April 13, 2016

Why are my messages not sending to the Socket.io room with this handler?

Leave a Comment

The way of sending a message to a specific room in Socket.io according to the docs seems really simple. In the following code is a handler, part of a bigger module, but the functionality should all be straightforward, especially since I'm showing the logs which expose what each variable represents:

module.exports.handle = function(client, data, socket, sessions, callback) {     debug.log('Client, ' + data.name + ' sent message:' + data.message);     debug.log(JSON.stringify(data, null, 3));     var sessionId = data.code;     var name = data.name;     var room = JSON.stringify(socket.sockets.adapter.rooms[sessionId]);     debug.log('Socket.io Room: ' + room);     socket.to(sessionId).emit('receive message', {         name : data.name,         message : data.message,         sender : client.id     }); }; 

Its appears clear in the following logs (based on the previous code) that the room I specify is a valid Socket.io room, so I dont understand why (in this instance) .to('M57VUYD1') is failing to send the message to the room. The logs seem to verify that M57VUYD1 in this case is a valid room.

[Debug][Send Message Event Handler]: Client, Jonathan sent message:hey [Debug][Send Message Event Handler]: {    "message": "hey",    "name": "Jonathan",    "code": "M57VUYD1" } [Debug][Send Message Event Handler]: Socket.io Room: {"sockets":{"57VUYD1":true,"D4N178C":true},"length":2} 

Is there something wrong with this module for it to not work as expected? Or have I used the correct syntax?

I was sending these messages globally (non room based) and the client was receiving the messages perfectly, so I don't think its a problem with my client side socket event listeners. And it doesn't seem to be my module for joining the room, because the logs show two clients being members of the room as they should be in the Socket.io's room data structure.

I added this problem in more detail on GitHub: https://github.com/socketio/socket.io/issues/2518

2 Answers

Answers 1

Based on what you said, including all of the renaming (which makes it a bit harder to guess), it seems that you have 2 connections: 57VUYD1 which appears to be the server, and D4N178C which I'm guessing is the client. So trying to connect to M57VUYD1 won't do much.

Try sending to the client's address instead:

socket.to('D4N178C').emit('receive message', {     name : data.name,     message : data.message,     sender : client.id }); 

It may be 57VUYD1 instead of D4N178C, as I'm not sure how you've defined things.

I'm also not sure what variable you have assigned that would let you get D4N178C, but you can just replace it with whatever works in the .to() above and should work.

Answers 2

The answer is this: Changing the socket IDs in Socket.io, as I learned on Stack Overflow, is something you can do, but what I had not realized is that doing so will break Socket.io in unknown ways. One of those ways is: The room system will no longer work.

In other words, dont change the socket IDs to ones of your own choosing, doing so isnt supported by the library.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment