Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing a 'heartbeat timeout' with websockets and SocketIO

I am using NodeJS and SocketIO for my websocket solution. It works fine, but after a few minutes, my socket server always times out with the following messages in my console:

debug - fired heartbeat timeout for client
info - transport end <heartbeat timeout>
debug - set close timeout for client
debug - cleared close timeout for client
debug - discarding transport

Here is my complete server.js file:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(3000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {

  socket.emit('news', { hello: 'from socket server' });

  socket.on('swipe', function (from, msg) {
     console.log('I received a private message by ', from, ' saying ', msg);
     socket.emit('swipe event received on server!');
  });

How can I prevent the timeouts from happening?

like image 420
Bachalo Avatar asked Dec 04 '13 22:12

Bachalo


People also ask

Does Socket.IO use WebSockets?

Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.

Does a WebSocket timeout?

A WebSocket times out if no read or write activity occurs and no Ping messages are received within the configured timeout period. The container enforces a 30-second timeout period as the default. If the timeout period is set to -1 , no timeout period is set for the connection.

How do I resolve a WebSocket connection error?

Solution 1Check that all the Bot Insight services are running. Check that your firewall settings are configured to accept incoming websocket data. Try to use a different web browser. Restart the Bot Insight Visualization and Bot Insight Scheduler services.

When should you not use a WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.


1 Answers

Check out the close timeout and heartbeat timeout options here

You can set these programmatically on the server via:

var io = require('socket.io').listen(80);
io.set('close timeout', 60);
io.set('heartbeat timeout', 60);

As for the design of your application, you should check out this question for whether or not you should change the timeout.

like image 84
Xinzz Avatar answered Sep 17 '22 03:09

Xinzz