SoftTimur
SoftTimur

Reputation: 5510

Activate idle/inactive connections

I have made a very simple app with socket.io. Now, it display all the connection ids: when we open a new browser tab, it adds an id; when we close a browser tab, it removes the id.

Now, I want to set one timeout event for any socket: If the button Keep alive! of a tab has not been clicked for 3 hours (that's maybe because the tab has not been re-visited for 3 hours), we consider the connection "semi-dead", and make its connection id grey in the page of other connections; when we click the button, the tab becomes alive and black in other pages, and restarts the countdown of 3 hours.

Does anyone know how to implement this?

Additionally, I have two optional questions about the relationships between idle/inactive connections, disconnect and tab-revisiting:

  1. If we don't re-visit an open tab for long time, will it be considered as a disconnect?

  2. Is it possible to set a re-visiting event on a browser tab?

My current back-end:

var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var ids = [];

server.listen(3000);

app.get('/', function (req, res) {
    res.sendfile(__dirname + '/index.html');
});

io.sockets.on('connection', function(socket) {
    ids.push(socket.id); 
    io.sockets.emit('ids', ids);

    socket.on('disconnect', function () {
        var index = ids.indexOf(socket.id);
        ids.splice(index, 1);
        io.sockets.emit('ids', ids);    
    })
});

front-end:

<button type="button" onclick="alert('Keep alive!')">Keep alive!</button>
<div id="ids"></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
    jQuery(function($) {
        var socket = io.connect();
        var $ids = $('#ids');

        socket.on('ids', function (data) {
            var html ="";
            for (i = 0; i < data.length; i++) {
                html += data[i] + "<br/>";
            };
            $ids.html(html);
        })
    });
</script>

Upvotes: 3

Views: 3434

Answers (1)

manishg
manishg

Reputation: 9818

Socket.io manages timeout using heartbeat (0.9.x versions) or using ping (version 1.x onwards). Once you set these timeouts on the server and if the client is socket.io compliant, the socket will never disconnect automatically. Of course it will disconnect if it loses network or a network timeout or someone disconnects it.

See the below text from the current socketio spec:

Among those options:

pingTimeout (Number): how many ms without a pong packet to consider the connection closed (60000) pingInterval(Number): how many ms before sending a new ping packet (25000).

The socket.io client will keep sending a ping or a heartbeat to make sure the connection is alive

.

So if your tab is open and if there is no activity, you will have to manage this at your application level and not at the socket.io level.

To do the same, you can use a timer and check when you received the data from a certain socket. The periodic timer can keep track of activity and change the status once your 3 hour period expires

Upvotes: 4

Related Questions