Where to instantiate a subscription that should be kept private for each user? #4114
-
On an app I'm working on, I need each user to instantiate a server-side subscription that uses some critical account information. I'd like to keep each user's subscription (and its "serverClient") completely private to their connection. I'd prefer if it was also inaccessible to other tabs within their browser. The current code for it looks something like this:
Both the "serverClient" and the actual subscription should be kept private to the user. So where would be the best place to instantiate this? After reading through the docs I see that there's some functionality in Rooms for each connections "default room" that's automatically accessed by that socket. Would this be the correct place for the subscription? If so, how can I instantiate the serverClient so that it's only accessible from within that room? I'll also need to teardown the subscription (and clear the serverClient) once the user disconnects. Thanks for any pointers. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 message");
}); |
Beta Was this translation helpful? Give feedback.
Hi! Topic subscriptions can easily be implemented with rooms:
Though I'm not sure what you mean by "private to their connection". You could store the subscription on the socket object: