socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

Add wildcard support for events

Open edmellum opened this issue 15 years ago • 132 comments

It would be great if you could use a wildcard to capture all events. For example:

client.on("*", function(data) {

}

edmellum avatar Jul 29 '11 11:07 edmellum

agreed.

EventEmitter2

joshrtay avatar Aug 05 '11 17:08 joshrtay

+1 This would allow us to create filters and what not for all events.

DennisRas avatar Aug 17 '11 15:08 DennisRas

  • another dependency
  • must be reflected at client side (code?)
  • should catchall be called before particular event? or in the order of definition? clarification needed
  • only sync behavior -- wouldn't it be better to introduce custom async filters for events?

dvv avatar Aug 17 '11 15:08 dvv

+1

only sync behavior -- wouldn't it be better to introduce custom async filters for events? @dvv

I'm quite interested in this idea.

some of the EE2 choices are not what I would consider ideal but I +1 the general idea of this, even if only "*" is supported

tj avatar Oct 03 '11 22:10 tj

truly catchall: manager.on("event", function(client, event, data) {} -- will also allow to reduce number of closures

dvv avatar Oct 04 '11 04:10 dvv

i don't remember any resistance to just adding a catchall listener, the only debate I remember was whether or not we use "*" or if we go with another method name like .addGlobalListener()

mikeal avatar Oct 04 '11 11:10 mikeal

+1 I too would need a way to intercept all events, and have the specific handler get a look at them, only once I'm done processing it. Mostly for logging purposes would this be needed. Socket.io logger currently only logs to the console, and in a very self righteous way. dvv -s approach is really to my liking. In fact maybe it would be a good idea to have us relay the event to any specific handler, and we just get all events, as described by dvv.

hdf avatar Oct 09 '11 12:10 hdf

Please get this issue in motion :) Would love to see this feature implemented.

DennisRas avatar Oct 09 '11 16:10 DennisRas

Well, ok, I just added EE2 to a branch in my fork: https://github.com/einaros/socket.io/commit/2107ff00f3ddf2d781d3e3c3b7dfb1fc990f7ec5

The branch is at: https://github.com/einaros/socket.io/commits/ee2

Thoughts are most welcome.

einaros avatar Oct 09 '11 17:10 einaros

if EE2 gets rid of the weird method names and adds .on('*') I'd +1 that

tj avatar Oct 09 '11 19:10 tj

I'm a -1 on EE2

It adds more bloat to the code, we also need to support it on the client side. Which means we would have to ship a extra 11.8 KB library (minified ~3.5kb). But with the up coming mobile market I would like to save as much bytes as possible.

If this is really only about having a wildcard / catch all listeners.. Then this should be done by overriding the existing emit function that just does one extra call to a all listener. This would be like a 3 - 5 LOC change (excluding comments ;)). And should be hidden behind a preference lock as it influence performance. EventEmitting is a hot code path and should always be as optimal and fast as possible. Adding wildcards will degrade performance.

3rd-Eden avatar Oct 09 '11 19:10 3rd-Eden

catch-all is definitely the more important part, it's easy enough to switch on the event after that if necessary

tj avatar Oct 09 '11 19:10 tj

Don't really care about wildcard's, or EE2, but a way, to intercept all events is a must.

hdf avatar Oct 09 '11 21:10 hdf

if EE2 ... adds .on('*') I'd +1 that

TJ you crazy bro...

server.on('foo.*', function(value1, value2) {
  console.log(this.event, value1, value2);
});

That's from the README of EE2. Naturally, the "foo." is optional.

if EE2 gets rid of the weird method names

I agree.

@pyrotechnick the EE2 .on('*') isnt a catch-all iirc

tj avatar Oct 09 '11 23:10 tj

* is not catch-all in the sense that it blindly catches all events but it does effectively catch all events since the pattern * matches all channels.

It's inefficient; but it does work as expected.

I was mistaken. You're right...

{EventEmitter2} = require 'eventemitter2'

emitter = new EventEmitter2 wildcard: on

emitter.on '*', ->
  console.log @event, arguments...

emitter.emit 'foo'
emitter.emit 'foo.bar'
isabel:hydrogen pyrotechnick$ coffee test.coffee 
foo

I almost prefer this behaviour when it comes to wildcards though.

When I think about all of this wildcard/"namespaced" events stuff it kind of makes me sad; JavaScript already has a fantastic way to match patterns -- they live in // or are constructed with RegExp. Is this just too slow?

Can I +1 the importance of this again. I'd love to see this in an upcoming release.

jrperina avatar Jan 25 '12 17:01 jrperina

Someone posted a solution here: http://stackoverflow.com/questions/8832414/overriding-socket-ios-emit-and-on/8838225

ghost avatar Jan 26 '12 09:01 ghost