Giovanni Bitliner
Giovanni Bitliner

Reputation: 2072

socket.io: disconnect event isn't fired

I have made a simple realtime visitor counter.

You can download it from this repository.

What happens is that disconnect event (even after browser closing) on server is never fired.

server.js is:

(function () {
var app, count, express, io;

express = require('express');
io = require('socket.io');

app = module.exports = express.createServer();

app.configure(function () {
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(require('stylus').middleware({
        src: __dirname + '/public'
    }));
    app.use(app.router);
    return app.use(express.static(__dirname + '/public'));
});

app.configure('development', function () {
    return app.use(express.errorHandler({
        dumpExceptions: true,
        showStack: true
    }));
});
app.configure('production', function () {
    return app.use(express.errorHandler());
});

io = require('socket.io').listen(app);

count = 0;

io.sockets.on('connection', function (socket) {
    count++;
    io.sockets.emit('count', {
        number: count
    });
});

io.sockets.on('disconnect', function () {
    console.log('DISCONNESSO!!! ');
    count--;
    io.sockets.emit('count', {
        number: count
    });
});


app.get('/', function (req, res) {
    return res.render('index', {
        title: 'node.js express socket.io counter'
    });
});
if (!module.parent) {
    app.listen(10927);
    console.log("Express server listening on port %d", app.address().port);
}

}).call(this);

Script on the client is:

    script(type='text/javascript')

        var socket = io.connect();

        socket.on('count', function (data) {
            $('#count').html( data.number );
        });

Upvotes: 41

Views: 52557

Answers (3)

Galen Long
Galen Long

Reputation: 3891

Just in case anyone else made this silly mistake: make sure that any socket middleware you've defined calls next() at the end, or else no other socket handlers will run.

// make sure to call next() at the end or...
io.use(function (socket, next) {
    console.log(socket.id, "connection middleware");
    next(); // don't forget this!
});

// ...none of the following will run:

io.use(function (socket, next) {
    console.log(socket.id, "second middleware");
    next(); // don't forget this either!
});

io.on("connection", function (socket) {
    console.log(socket.id, "connection event");
    socket.once("disconnect", function () {
        console.log(socket.id, "disconnected");
    });
});

Upvotes: 3

NoNameProvided
NoNameProvided

Reputation: 8987

From Socket.IO 1.0 the io.engine.clientsCount property is available. This property tells you how many open connection does your app currently have.

io.sockets.on('connection', function (socket) {
    io.sockets.emit('count', {
        number: io.engine.clientsCount
    });

    socket.once('disconnect', function () {
        io.sockets.emit('count', {
            number: io.engine.clientsCount
        });
    });
});

Note: Use .once instead of .on and the listener will be removed automatically from the socket what is good for us now, because the disconnect event is only fired once per socket.

Upvotes: 3

swiecki
swiecki

Reputation: 3483

Put your on disconnect code inside your on connect block and edit it a bit like so:

io.sockets.on('connection', function (socket) {
    count++;
    io.sockets.emit('count', {
        number: count
    });

    socket.on('disconnect', function () {
        console.log('DISCONNESSO!!! ');
        count--;
        io.sockets.emit('count', {
            number: count
        });
    });
});

This way you're detecting when a specific socket (specifically the socket you pass to your anonymous function that is run on connection) is disconnected.

Upvotes: 57

Related Questions