I want to implement multiple chat windows on one page (like facebook for e.g.). Currently using "rooms", simplified code:
[client]
socket.emit('join', 'room1');
socket.emit('join', 'room2');
[server]
socket.on('join', function(room)
{
socket.join(room);
});
so each separate room on client, it is a separate class that handles the chat logic, it must receive events from server only for connected room. But if it is one page, then one socket connection used, so if one socket will connect to multiple rooms, then subscribing to one room, will also receive events from another room. How should I separate this?
I'm thinking about these ways:
- Force new socket connection for each room (so 10 chats = 10 sockets), I think it is not good idea, because of highload (if I'm not right, please correct me)
- Prefix for client events, e.g if I want to handle events for specific room I will subscribe like this:
[client]
var room = 'room1';
socket.on(room + '.new_message', function()
{
// append message to chat
});
[server]
io.sockets.in('room1').emit('room1.new_message', 'some message');
is this a good idea?
If it is other ways to do it, please share with me.