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.
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?