Having trouble managing multiple rooms with node.js and socket.io
socket.on('join_room', function (data) {
socket.join(data);
});
socket.on('send_msg', function (data) {
io.sockets.in(theroom).emit('message', data);
});
If I set the room in a variable (i.e theroom) then naturally that gets over written with the latest room, how can I have multiple rooms?
I solved this by writing a function that sends to rooms:
listen: (server, callback) =>
@server = server
@logger.debug 'SDL: starting up socket'
# @socketTool is object returned by socket io require
@listener = @socketTool.listen(server, log: true, 'log level': @logLevel )
emitToRoom: (room, type, data, callback) =>
@logger.debug "all rooms: #{@utilLib.inspect @listener.sockets.manager.rooms}"
@listener.sockets.in(room).emit(type, data)
callback() if callback?
I think the thing that you're missing is that the individual socket doesn't understand other rooms, you need to handle that at the listener level.