Only emit event 'x' to clients that explicitly listen for it #3954
Unanswered
ChrisKatsaras
asked this question in
Q&A
Replies: 1 comment 5 replies
-
For your use case, I think the best solution is to use the rooms to implement subscriptions: Client-side: socket.on("connect", () => {
socket.emit("subscribe", "event x");
}); Server-side: io.on("connection", (socket) => {
socket.on("subscribe", (topic) => {
socket.join(topic);
});
// and then later
io.to("event x").emit("hello", "only to subscribers");
}); |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, I'm dealing with an issue where I have a room with socket connections and I only want to emit
event 'x'
to socket connections that are explicitly listening to it on the client.I've noticed that regardless of whether or not the client is listening for it, it still gets sent over the wire. (It doesn't get handled, but I can still see it being sent over the wire in my console).
To give a bit more detail, given the example room with socket connections:
connection 1
connection 2
connection 3
If only
connection 3
is listening forevent 'x'
, I would just wantconnection 3
to receiveevent 'x'
Right now, all receive the event while
connection 3
is the only one that explicitly handles it.Am I missing something or is it not possible to emit an event to a room of connections and have it filtered down?
Any help is greatly appreciated :)
Beta Was this translation helpful? Give feedback.
All reactions