Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io specifying heartbeat

I can't seem to find out how to set the heartbeat options for socket.io? I'd love to be able to specify what happens when a "timemout" happens, and when the server hasn't gotten any response from the client for some time.

For some reason the disconnect event isn't always firing, even though it has passed over an hour without the client not being connected. This often happens with the standalone Java socket.io client

like image 586
Kasper Rynning-Tønnesen Avatar asked Jun 29 '15 15:06

Kasper Rynning-Tønnesen


People also ask

Does Socket.IO use long-polling?

The bidirectional channel between the Socket.IO server (Node. js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Is Socket.IO difficult?

All in all, the Socket.io code was incredibly simple.

What is pingTimeout?

pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000) pingInterval (Number): how many ms before sending a new ping packet (25000).

Does Socket.IO guarantee order?

This is an old post, but it's worth noting that Socket.io over WebSockets actually does guarantee event ordering is maintained. This is because TCP itself, which is the underlying technology for WebSockets & HTTP guarantees packet ordering to be maintained.


1 Answers

Socket.IO uses engine.io, which allows you to specify ping interval and ping timeout, like so:

var app = require('express')();
var http = require('http').Server(app);

var io = require('socket.io')(http, {'pingInterval': 2000, 'pingTimeout': 5000});

(2 second interval and 5 second timeout is just an example. It might be to frequent for your use case)

like image 166
Tholle Avatar answered Sep 28 '22 08:09

Tholle