to() not sending message to occupants of room, when the Socket data structure shows the room is occupied by 2 clients
This could be something Im doing wrong, or it could perhaps be a bug because I modified the IDs of each client that connects to the socket with my own unique ID.
First of all, see my Stack Overflow question which explains this in a shorter version:
http://stackoverflow.com/questions/36405606/why-are-my-messages-not-sending-to-the-socket-io-room-with-this-handler
Im working on a session based app where the sessions are shared with a unique code. To do this, I decided to modify each client's ID with my own choice of a unique ID format (which as I understand it is a standard thing you can do in Socket.io without breaking anything) because I wanted a shorter ID that a person could type, because I was going to use this (socket) ID of the session creator to name the Socket.io room for that session. But then I learned that for private messaging, Socket.io has each member join a room with its own name. So I couldnt name the session's room with the client's ID or I would no longer be able to message the user specifically, because the creator of the session would have a room now with multiple members. So I named the room the client's socket ID with a letter "M" added to the front of the ID.
I tested my code sending messages globally, without using the room, and everything was fine. But when I changed the following socket handler to send the messages only to the room, it doesnt send to any of the connected clients. I wonder if this is a bug because of me changing the socket IDs of each connected client.
So I'll show my code and you can tell me if Im the one doing it wrong or there's a bug, or perhaps I misunderstood something:
First, my (server side) handler for an incoming message, and this is what is executed when a certain message comes over the socket, in this case message, and please note that what you guys in your documents call "io" I named in this code socket, and what you guys call "socket" I named client:
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
});
};
And the logs from this code would seem to verify that the room does exist and there are two occupants, neither of which are receiving the message:
[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}
Also, if you wanted to check the handler that has them join or create the room, here you go:
Create Session handler:
module.exports.handle = function(client, data, socket, sessions, callback) {
var sessionId = 'M' + client.id; // remember "client" is what you call "socket" in your docs
client.join(sessionId);
};
Join Session handler:
module.exports.handle = function(client, data, socket, sessions, callback) {
var sessionId = data.code;
var name = data.name;
client.join(sessionId);
socket.to(sessionId).emit('new member', { //no one receives this either
session : sessions[sessionId],
clientId : client.id,
newMember : data.name
});
};
And here's where they recieve new IDs upon connecting:
module.exports.socket = function(server) {
var sessions = {};
var idList = {};
var socket = require(dirPath + '/node_modules/socket.io')(server);
socket.on("connection", function(client) {
debug.log("Client connected to socket: " + client);
convert(client, sessions, idList);
handler.handle(client, socket, blueprint, sessions);
});
};
And finally here's the convert function in case it matters how I generate my unique IDs:
function generateID(length) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
for (var i = 0; i < length; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
module.exports.convert = function(client, sessions, idList) {
var id = generateID(7);
while (idList[id]) {
id = generateID(7);
}
client.id = id; // here's where the change occurs
idList[id] = true;
};
So I removed alot of code relevant only to my application from these handlers, so don't try to make sense of them as completed modules, but the Socket.io parts, thanks to you guys, seem really simple and straight forward. However, as shown in in my first logs, a room seems to exist with the occupants, but the to method, verified as pointing to the correct destination in the logs I showed earlier, is not sending the message to the clients that are shown in the room.
It just seems to me like maybe there is a bug where Socket.io's room functionality breaks when the IDs of the clients are changed.
On Stack Overflow I was told that socket IDs cannot be edited in Socket.io without breaking internal mechanisms. So that answers this.