3

How can I configure the option 'close timeout' with the code below ?

 var app = require('express')();
 var server = require('http').Server(app);
 var io = require('socket.io')(server);
 ...
 server.listen(port, ip);     

I read the doc about socket.io and I found that :

 var socket = require('socket.io')({
     // options go here
 });

but I can't add options because I'm using the server variable.

Thanks.

user3146857
  • 31
  • 1
  • 3

2 Answers2

2

Have you seen this ?

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

Maybe something like

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
io.set('close timeout', 60);
server.listen(port, ip); 

Edit: This is a long shot but:

var app = require('express')();
var server = require('http').Server(app);
server['close timeout'] = 60;
var io = require('socket.io')(server);
server.listen(port, ip); 

Edit: Found this on socket.io docs:

// pass a server and the `serveClient` option
var io = require('socket.io')(http, { serveClient: false });

So, what about this?

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server, { 'close timeout': 60});
server.listen(port, ip); 

Yet another Edit: In docs again:

The same options passed to socket.io are always passed to the engine.io Server that gets created. See engine.io options as reference.

pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000)

Can you try this?

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server, { pingTimeout: 60000});
server.listen(port, ip); 
Community
  • 1
  • 1
Bruno Camarneiro
  • 545
  • 4
  • 19
  • 1
    io.set() is deprecated since socket.io 1.0, I can't use this – user3146857 Oct 20 '14 at 14:13
  • 2
    'server['close timeout'] = 60;' is not a bad idea but it doesn't work. I'm wondering if I do things correctly -_-' – user3146857 Oct 20 '14 at 14:31
  • 1
    I already tried 'var io = require('socket.io')(server, { 'close timeout': 60});' => didn't work :( it's weird and that is why I'm wondering if I do something wrong ... – user3146857 Oct 20 '14 at 14:52
  • I'm no socket.io expert but I went and checked [it's source](https://github.com/Automattic/socket.io/blob/master/lib/index.js) and I can't find 'close timeout'. If this option was supressed in the new version there should be a better way to achieve what you are trying to do... – Bruno Camarneiro Oct 20 '14 at 14:58
  • I'm just trying to reduce the time between disconnection (by closing page for example) and when the disconnect event is fired. Locally, it's instant but on a remote server, this time is fixed by default to 60 seconds (too much ...). And the 'close timeout' option was the better (only ?) option I found. – user3146857 Oct 20 '14 at 15:09
  • I found something else. Check my last edit. (I'm not really sure if it works. I'm just going through the docs) – Bruno Camarneiro Oct 20 '14 at 15:24
  • And maybe consider playing with pingInterval too – Bruno Camarneiro Oct 20 '14 at 15:26
  • Thanks for your help :) using just pingTimeout doesn't work. I'll try adding pingInterval later. I'll tell you if it's ok – user3146857 Oct 20 '14 at 16:16
  • So, after reading that, pingTimeout is an option for engine.io but I use socket.io. I found same option, according 'var io = require('socket.io')(server);' => 'io.engine.pingTimeout' and 'io.engine.pingInterval' but still doesn't work ... – user3146857 Oct 20 '14 at 22:46
  • 1
    @user3146857 - `socket.io` is built on top of `engine.io`. `engine.io` is the transport. – jfriend00 Oct 21 '14 at 00:02
0

According to the latest version. { pingTimeout: 60000} works fine for me.

And io.set('heartbeat timeout', 10) works as well, but set will be removed in the future.

xinyuan
  • 1
  • 2