Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io - disconnect event doesn't fire in xhr-polling

In socket.io the disconnect event doesn't fire when the transport xhr-polling is active. If I switch the transport to websockets it works fine, but in xhr-polling its doesn't work.

/* Basics */
var express = require('express'),
    app = express(),
    server = require('http').createServer(app),
    io = require('socket.io').listen(server);

server.listen(1337, null);

io.set('transports', ['xhr-polling']);

// routing
app.get('/', function (req, res) {
    res.sendfile("index.html");
    app.use(express.static(__dirname));
});

io.sockets.on('connection', function (socket) 
    socket.on('disconnect', function() {
        console.log('LOL');
    });
});

In the following code the disconnect doesn't fire, but if I delete the line -
io.set('transports', ['xhr-polling']);
It works perfectly, so why it doesn't work with xhr-polling? but only with websockets?

How can I fix this? Am I missing something?

Thanks in Advance ;)

like image 245
julian Avatar asked Nov 04 '22 00:11

julian


1 Answers

It's impossible to immediately detect disconnects using XHR-Polling since the client connects repeatedly for each message.

Socket.io gets around this by setting an inactivity timeout on xhr-polling sockets.

This is the relevant documentation: https://github.com/LearnBoost/Socket.IO/wiki/Configuring-Socket.IO

See the heartbeat interval configuration option. The default is 25 seconds.

Websocket maintains a persistant tcp socket between the browser and the server so socket.io can usually detect immediately when the socket is disconnected.

like image 159
secretmike Avatar answered Nov 11 '22 06:11

secretmike