4

So this is part of a larger app, that I was trying to get running with cluster, and socket.io-redis so it would work across multiple nodes and machines if I need to scale in the future.

I seem to be getting severe issues with some clients not being able to maintain connections. Some clients do this persistently (mostly iOS devices, which I imagine must be using polling transport), but others sometimes work, and sometimes dont (which again I'm attributing to using polling).

I stripped back everything, and turns out cluster wasn't the issue.

Anyways my server.js

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

var port = process.env.PORT || 3800;

/* -------------------------------- */

io.on('connection', function(socket){
    var datetime = new Date().toLocaleString();
    console.log(datetime+' - '+'a user connected - '+socket.id);
});

/* -------------------------------- */


http.listen(port, function() {
    var datetime = new Date().toLocaleString();
    console.log(datetime+' - '+'listening on *:' + port);
});

and output in the console

Wed Mar 11 2015 16:22:53 GMT+0000 (GMT) - listening on *:3800
Wed Mar 11 2015 16:22:54 GMT+0000 (GMT) - a user connected - YrLUOrgfpnQVwoWMAAAA
Wed Mar 11 2015 16:22:58 GMT+0000 (GMT) - a user connected - nH6x-97uf3iR2LgNAAAB
Wed Mar 11 2015 16:23:01 GMT+0000 (GMT) - a user connected - VsrFO31elPyKfQJcAAAC
Wed Mar 11 2015 16:23:08 GMT+0000 (GMT) - a user connected - 7xuKZ6aykYIDcxN-AAAD
Wed Mar 11 2015 16:23:15 GMT+0000 (GMT) - a user connected - ZsmZtFHmLrH1DxXWAAAE
Wed Mar 11 2015 16:23:22 GMT+0000 (GMT) - a user connected - HFUApMwwFZallJnQAAAF

This just goes on indefinitely. I am testing it with an iPad, with the client loading from within an appgyver supersonic app (cordova and javascript).

I get the same issue when using a fork of the socket.io chat example, modified for cluster use - https://github.com/evilstudios/chat-example-cluster

My Question: Why are some clients unable to retain a connection? How do I fix these issues with the polling transport not maintaining a single session?

Horse
  • 3,023
  • 5
  • 38
  • 65
  • I'd suggest you look at a server-side network trace and see what is actually going on. You want to know if the client is requesting a webSocket connection and then losing it or if the client and server are not agreeing to upgrade to webSocket. All webSocket connections start with an http request and then the two sides are supposed to agree to upgrade to a persistent webSocket connection. – jfriend00 Mar 11 '15 at 17:15
  • 1
    but surely if its falling back to polling it should still essentially work? eg connect and retain the same session id for each poll – Horse Mar 12 '15 at 10:00
  • The first thing to do is (as I said in my earlier comment) to find out what is actually happening on the network and then go from there. You can theorize that a session should be maintained, but until you know what's actually happening on the network, you really don't know what to look at next. – jfriend00 Mar 12 '15 at 21:15
  • @Horse Were you able to solve the problem? I am facing the same issue with no help! – Ahsan Jul 25 '17 at 05:32
  • @Raptor it was a while ago, but I vaguely remember upgrading (or downgrading) version and it just went away. Also iirc it didn't like it when the server came up with existing connections. Hope that helps, for posterity let us know :) – Horse Jul 28 '17 at 15:54
  • 1
    @Horse thanks for your reply! I also managed to get it fixed by upgrading to the latest version! :) – Ahsan Jul 28 '17 at 16:33

2 Answers2

1

you have to update your socket.io front and back packages otherwise you will have malfunctions for the way of transporting the socket, the front will use polling and therefore you will have mass socket disconnection connections....

Solution: update the versions of your socket.io packages front and back

Canonne Gregory
  • 378
  • 4
  • 9
0

It could be due to node.js single-threaded architecture.

Check out the answer here: https://stackoverflow.com/a/51524799/996926

advncd
  • 3,787
  • 1
  • 25
  • 31