Is there a way to "transform" an event before being emitted to a member of a room? #4050
-
As an example of what I mean: Say we have a user with the type: interface User {
id: string;
name: string;
bankBalance: number;
} Now something happens on the server that changes the user's name. We update the database and emit the event "updated" to the "user:abc" room with the payload Now say we have connectionA and connectionB. Both are joined to the "user:abc" room. What I want is connectionA to see the full payload (because of their admin role for example) but I want connectionB ONLY to see a restricted version of the payload: Is this possible? Im imagining some sort of "transformer" or "middleware" that can intercept the event before its sent along the socket. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I think the easiest way to achieve this is to use two rooms, one for the full payload and one for the restricted one: // full payload
io.to("user:abc:admin").emit("updated", { id: "abc", name: "newname", balance: 123456 });
// restricted payload
io.to("user:abc").emit("updated", { id: "abc", name: "newname" }); Reference: https://socket.io/docs/v4/rooms/ This can also be achieved with a namespace: io.of("/admin").to("user:abc").emit("updated", { id: "abc", name: "newname", balance: 123456 }); Reference: https://socket.io/docs/v4/namespaces/ |
Beta Was this translation helpful? Give feedback.
I think the easiest way to achieve this is to use two rooms, one for the full payload and one for the restricted one:
Reference: https://socket.io/docs/v4/rooms/
This can also be achieved with a namespace:
Reference: https://socket.io/docs/v4/namespaces/