Skip to content

Socket io group chat logic #5132

Discussion options

You must be logged in to vote

Hi!

You can take a look at our chat platform project. In this project, we use your 2nd option:

  • the user join all the chat rooms it's part of:
  io.use(async (socket, next) => {
    // [...]

    let channels;

    try {
      channels = await db.fetchUserChannels(socket.userId);
    } catch (e) {
      return next(new Error("something went wrong"));
    }

    channels.forEach((channelId) => {
      socket.join(channelRoom(channelId));
    });

    // [...]

    next();
  });

Source: https://github.com/socketio/socket.io-chat-platform/blob/2a32486ff621a61648a3b28fad751e94a6cf2304/server/src/index.js#L92-L94

  • then the message is simply sent to the chat room:
socket.broadcast
  .to(chann…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@emirhanyagci
Comment options

@darrachequesne
Comment options

@emirhanyagci
Comment options

Answer selected by emirhanyagci
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants