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
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.
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With