Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io Client: respond to all events with one handler?

Is it possible to have a socket.io client respond to all events without to have specify each event individually?

For example, something like this (which obviously doesn't work right now):

var socket = io.connect("http://myserver");  socket.on("*", function(){   // listen to any and all events that are emitted from the   // socket.io back-end server, and handle them here.    // is this possible? how can i do this? }); 

I want this callback function to be called when any / all events are received by the client-side socket.io code.

Is this possible? How?

like image 840
Derick Bailey Avatar asked May 01 '12 22:05

Derick Bailey


People also ask

How many concurrent connections can Socket.IO handle?

Once you reboot your machine, you will now be able to happily go to 55k concurrent connections (per incoming IP).

How many messages per second can Socket.IO handle?

Both server and client node processes use 95-100% of a CPU core each. So pure throughput looks ok. I can emit 100 messages per second to 100 local clients at 55% CPU usage on the server process.

Is Socket.IO better than WebSocket?

As said before, Socket.IO can fall back to technologies other than WebSockets when the client doesn't support it. If (for some reason) a WebSocket connection drops, it will not automatically reconnect… but guess what? Socket.IO handles that for you! Socket.IO APIs are built to be easier to work with.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).


1 Answers

Updated solution for socket.io-client 1.3.7

var onevent = socket.onevent; socket.onevent = function (packet) {     var args = packet.data || [];     onevent.call (this, packet);    // original call     packet.data = ["*"].concat(args);     onevent.call(this, packet);      // additional call to catch-all }; 

Use like this:

socket.on("*",function(event,data) {     console.log(event);     console.log(data); }); 

None of the answers worked for me, though the one of Mathias Hopf and Maros Pixel came close, this is my adjusted version.

NOTE: this only catches custom events, not connect/disconnect etc

like image 167
Flion Avatar answered Sep 22 '22 11:09

Flion