1

I'm using Socket.io 1.0 on my project and I would like to bind any event on the server, something like:

socket.on('*')

so I can do that on my client:

socket.emit('forum.post')

And the server would call the function "post" of the object "Forum". That would be to have a better organization in my project. With some methods I found on the web, I would do that to override socket.io function:

 var onevent = socket.onevent;
 socket.onevent = function() {
     console.log('***','on',Array.prototype.slice.call(arguments));
     onevent.apply(socket, arguments);
 };

but the socket.on('*') is never called.

Madnx
  • 1,532
  • 4
  • 16
  • 23
  • check this thread http://stackoverflow.com/questions/10405070/socket-io-client-respond-to-all-events-with-one-handler – Dslayer Sep 13 '14 at 13:15
  • Should be easy using https://github.com/turbonetix/socket.io-events – vesse Sep 15 '14 at 06:36

1 Answers1

1

Solution that worked for me (socket-io v1.3.5).

var Emitter = require('events').EventEmitter;
var emit = Emitter.prototype.emit;
// [...]
var onevent = socket.onevent;
socket.onevent = function (packet) {
    var args = packet.data || [];
    onevent.call (this, packet);    // original call
    emit.apply   (this, ["*"].concat(args));      // additional call to catch-all
};

Solution by @Matthias Hopf issue: Socket.io Client: respond to all events with one handler?

Community
  • 1
  • 1