Skip to content

dynamic socket.on and middleware #4185

Answered by darrachequesne
artstylee asked this question in Q&A
Discussion options

You must be logged in to vote

the problem is that the users dynamically add/delete channels..

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

before someone emit a message a want to check the authorization, if user are able to add/messages to this channel

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.authorizedCha…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@artstylee
Comment options

@darrachequesne
Comment options

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