Skip to content

Where to instantiate a subscription that should be kept private for each user? #4114

Discussion options

You must be logged in to vote

Hi! Topic subscriptions can easily be implemented with rooms:

io.on("connection", (socket) => {
  // from the server side
  socket.join("the topic");

  // or from the client side
  socket.on("new subscription", (topic) => {
    socket.join(topic);
  });

  // and then
  io.to("the topic").emit("new message");
});

Though I'm not sure what you mean by "private to their connection". You could store the subscription on the socket object:

io.on("connection", (socket) => {
  socket.subscriptions = new Set;

  socket.on("new subscription", (topic) => {
    socket.subscriptions.add(topic);
  });

  // and then
  socket.emit("new message");
  // which is equivalent to
  io.to(socket.id).emit("new…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@scalemaildev
Comment options

Answer selected by scalemaildev
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