Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen for events globally? #2626

Closed
shirotech opened this issue Jul 29, 2016 · 4 comments
Closed

How to listen for events globally? #2626

shirotech opened this issue Jul 29, 2016 · 4 comments

Comments

@shirotech
Copy link

Hi there, I'm trying to make a global event that can be listened from any connection. A use case would be so that for each connection doesn't need a separate handler. So far the only "global" event is "connection", I want to be able to listen to any event globally. Is this possible?

@MaffooBristol
Copy link

Yeah this is one that's been discussed a huge amount over many years: #434

This is the best solution at the moment imho: https://www.npmjs.com/package/socketio-wildcard

@shirotech
Copy link
Author

Hmm, thanks for the reply, but I don't think it's the same thing I'm after. It is indeed a good feature to have wildcards, but what I'm really after is a many-to-one handler.

e.g.

io.on('login', (data)=> {
  // do login stuff
});

instead of

io.on('connection', (socket)=> {
  socket.on('login', (data)=> {
    // do login stuff
  });
});

As you can see the current structure creates an overhead of a nested handler.

@MaffooBristol
Copy link

Ahh. No, as far as I'm aware, connect and disconnect events are very much fundamental to the way client connections work. How could a user login without being connected first, for example? Nothing can be sent outside of the handshake.

@barraponto
Copy link

If you don't like the nesting, consider io.use (http://socket.io/docs/server-api/#namespace#use(fn:function):namespace).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants