Note that there are some explanatory texts on larger screens.

plurals
  1. POSending to Subset of Users in Room/Channel with Socket.IO
    primarykey
    data
    text
    <p>I'm building a web application with Node (Express) and Socket.IO that has chat functionality. Because opening a new tab on a page establishes a new socket connection, I need to group all instances of a single user into their own room based on the Express session ID to enable all messages aimed at said user to appear in all duplicate tabs. This is in addition to any other room/channel they might already be logged into. At a minimum, users are subscribed to two chatrooms: the actual "real" room, and their own channel using the sessionID.</p> <p>The problem is that all of the sockets for my sessionID are also in the more general room (and need to be to get messages from other users). When I send out the general chat message I'd like to omit any sockets corresponding to the sending user, as they have already received the message through their own channel. I've gone ahead and made a hash of arrays containing lists of socketIDs for that session, keyed on the sessionID. I've seen a few different syntaxes for specifying exception lists, but none seem to work for me.</p> <p>The relevant code, with certain parts omitted for brevity: </p> <pre><code>var sessionSockets = {}; io.sockets.on('connection', function(socket){ if(!io.sockets.manager.rooms["/" + sessionID]) { sessionSockets[sessionID] = []; //send message indicating log on to all sockets except for my session } socket.join(sessionID); //create private channel for all sockets of the same sessionID sessionSockets[sessionID].push(socket.id); socket.on('chat', function(data){ var payload = { message: data.msg, from: data.user }; //send back as personal message to all sockets for this session io.sockets.in(sessionID).emit('me',payload); //send to everyone else as regular message; WHAT SYNTAX? io.sockets.in('').except(sessionSockets[sessionID]).emit('chat', payload); } } </code></pre> <p>tl;dr: How can I send a message to a subset of users in a channel/room without manually doing a comparison of arrays?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload