Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observe the ping of socketio client at server side

I know that the socketio client sends a ping at every 25 secs (heartbeat interval) and gets a response back from the server. I want to print this ping from client on the server side and also if possible capture the ping response sent back by the node server.

I tried using

socket.on("ping", function(data) {
    console.log("received ping");
});
socket.on("pong", function(data) {
    console.log("received pong");
});

both on client and the server side but i am not able to capture them

like image 995
Ajith Avatar asked May 13 '15 06:05

Ajith


People also ask

How do I send an event in socketio?

Socket.IO is a bidirectional protocol, so at any time the server can send an event to its connected clients. The socketio.Server.emit () method is used for this task: sio.emit('my event', {'data': 'foobar'}) Sometimes the server may want to send an event just to a particular client.

Is it better to set Ping timeout on server or client?

It might be better to set different pingTimeouts on the server and on client. On server, pingTimeout (+pingInterval) means the idle time the server will keep the socket up. This can be quite long, as the client can sleep while the connection is still kept al

What is the difference between socketio server and socketio asyncserver?

The socketio.Server () class creates a server compatible with the Python standard library. The socketio.AsyncServer () class creates a server compatible with the asyncio package.

What is the socket Io server?

While not comparable to eventlet and gevent in terms of performance, the Socket.IO server can also be configured to work with multi-threaded web servers that use standard Python threads. This is an ideal setup to use with development servers such as Werkzeug.


1 Answers

This snippet can run on the server. The documentation for the underlying engine.io server socket specifies the parameters for the event listeners packet and packetCreate, and that object is located on socket.conn:

socket.conn.on('packet', function (packet) {
  if (packet.type === 'ping') console.log('received ping');
});

socket.conn.on('packetCreate', function (packet) {
  if (packet.type === 'pong') console.log('sending pong');
});

Turns out the engine.io documentation for these events are wrong. The arguments are not type and data, it is a single argument packet that contains the properties type and data, according to the source for the packet event and the packetCreate event.

like image 147
Patrick Roberts Avatar answered Sep 21 '22 11:09

Patrick Roberts