dynamic socket.on and middleware #4185
-
hi guys! im doing a copy of discord on socket io
the problem is that the users dynamically add/delete channels.. the questions is :
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think you can either use a common event name: socket.on("message", (channel, msg) => {
// ...
}); or use a catch all listener: socket.onAny((event, ...args) => {
// ...
}); Reference: https://socket.io/docs/v4/server-api/#socketonanycallback
You could store the list of authorized channels when the user first connects: io.on("connection", async (socket) => {
socket.authorizedChannels = await fetchAuthorizedChannels();
// and then
socket.on("message", (channel, msg) => {
if (socket.authorizedChannels.includes(channel)) {
// ignore the message or close the connection
}
});
}); |
Beta Was this translation helpful? Give feedback.
I think you can either use a common event name:
or use a catch all listener:
Reference: https://socket.io/docs/v4/server-api/#socketonanycallback
You could store the list of authorized channels when the user first connects: