Ad

Socket.io Stops Working After Multiple Refreshes

- 1 answer

I have a socket.io connected to a https server listening for javascript to emit information to it. I am finding that when I refresh, the socket is recording the connection - so if i console.log something in the on('connection', function(socket) { } part of the code, I will see a response in the console.

What I am finding though, is that on the server side, the socket.on() calls are not picking up anything.

So if I have a line in Javascript which is socket.emit('ping', msg), 'ping' will work on the server side for the first 9-10 refreshes, and then on the 11th refresh it will stop working - and it will stop working on all devices and all browsers. So if I open another browser and try to load it up, it won't work either.

I have checked to see if there are multiple connections building up or something like that, but I am not seeing anything. Any idea why this weird behaviour is happening?

Server Side

io.use(function(socket, next) { 
    // Authentication of the user is happening here - checking for cookies, sessions, etc.
}).on('connection', function(socket) {

    // Setting up some variables and checking other stuff
    var socketID         = socket["id"];
    var username         = socketUsers[socketID];
        locked[socketID] = {};

    // Checking for the username
    if(username == '' || typeof username == "undefined") {
        // Emit some stuff back to Javascript
        io.to(`${socketID}`).emit('criticalError', true);
        return false;
    }
    else {
        // We have a valid login, so connect to the server
        ++connected;

        // This line will console log every time I refresh
        console.log("Some text");

        socket.on('ping', function(msg) {
            // This line will console log for the first 9-10 refreshes, and then it suddenly stops working. Any ideas why?
            console.log('Server was pinged by ' + username);
        });

        // This line will console log every time I refresh
        console.log("Some other text");
    }
});

Client side

var socket = io('https://example.com:1337');
$(document).ready(function() {
    socket.emit('ping', true);
});
Ad

Answer

This is an interesting problem that you are facing. There are multiple points to check on - However the first problem I would suggest is with the below code:

var socket = io('https://example.com:1337');
$(document).ready(function() {
    socket.emit('ping', true);
});

If this is the exact code on the client side, chances are that the socket.emit is getting called before the connection handshake goes through. This can be random as sometimes the document ready event might be delayed. Try setting a timeout for the emit event -

var socket = io('https://example.com:1337');
$(document).ready(function() {
    setTimeout(function(){socket.emit('ping', true);},2000);
});

and give it a try. If this works, you found the problem. Next step is to ensure authentication is working correctly - Without the complete code, advising on this is difficult. Feel free to edit the question and share the code if your issue isn't resolved.

Ad
source: stackoverflow.com
Ad