node.js - get disconnect event with socket.io with xhr-polling

Can someone tell me how to get disconnect event with socket.io with xhr-polling in node.js please?

This example not working with xhr-polling but works with websockets..

/* 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));
});

var online_clients = 0;

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

So how to get disconnect event with xhr-pooling?

Thanks in advance ;)

The xhr-polling implementation in socket.io does not emit disconnect event when the connection is closed, see github issue #431.

You can ask socket.io's client to notify the server about disconnect by turning on the sync disconnect on unload flag:

// the browser (HTML) side
var socket = io.connect('http://localhost', {
  'sync disconnect on unload': true
});

Warning: this option can worsen the user experience when the network and/or your server are slow, see this pull request for more information.

Related questions: