5

I have setup a Node.JS server with socket.io on a VPS and I broadcast every 10 seconds the number of connected clients to all. This usually works fine, though often times, the connection can't be established and I get this error (I changed the IP a bit):

GET http://166.0.0.55:8177/socket.io/1/?t=1385120872574

After reloading the site, usually the connection can be established, though I have no idea why the failed connection happens in the first place, also I don't know how to debug the socket.io code. Sometimes I can't connect to the server anymore and I have to restart the server.

Additional information:

  • My main site runs on a different server (using a LAMP environment with CakePHP) than the Node.js server.
  • I use forever to run the server
  • I have a lot of connected clients (around 1000)
  • My VPS has 512 MB Ram and the CPU is never higher than 25%
Christian Strang
  • 8,470
  • 5
  • 42
  • 53
  • Did your node process CPU come up to 100%? (Just node CPU not entire server) Because Node.js is single thread. – Paiboon Panusbordee Nov 22 '13 at 12:43
  • Mh.. not sure how to check this, I'm really new to the server side :/ – Christian Strang Nov 22 '13 at 12:51
  • 1
    Try "top" command in linux shell and find node.js process. – Paiboon Panusbordee Nov 22 '13 at 12:56
  • ah, thank you so much! Currently its at 10%, though it jumps to 20% and then back. Right now there is no issue with the service (I get the connected clients) but will take a look at TOP if the script starts to fail again :) – Christian Strang Nov 22 '13 at 13:01
  • The connection currently works every second time (if I reload, it works, reloading again and it doesn't). The node process uses around 15% CPU and 13% MEM. There are 2 other processes that use both less than 1% CPU & MEM. – Christian Strang Nov 22 '13 at 13:12
  • With `socket.on('error', function (err) { console.log("Socket.IO Error"); console.log(err); });` I receive the Message "Socket.IO Error" though I don't receive the error that triggered the function :/ – Christian Strang Nov 22 '13 at 13:30

1 Answers1

3

After top command, try:

socket.on('error', function (err) { 
   console.log("Socket.IO Error"); 
   console.log(err.stack); // this is changed from your code in last comment
});

Also, you could try a the slower transport. Socket.io use Websocket by default but if your server cannot allocate enough resource, you can try another transport which is slower but use less resources

io = socketIo.listen(80);
io.set('transports', ['xhr-polling']);
Paiboon Panusbordee
  • 781
  • 2
  • 10
  • 26
  • when I change the code to `console.log(err.stack)` I get an undefined. Do I have to send something from the server to make this work? – Christian Strang Nov 25 '13 at 10:57
  • `io.set('transports', ['xhr-polling']);` seems to have fixed the issue, therefore I marked the answer as correct :) Is there a way to debug the "cannot allocate enough resources" issue? Would a bigger/stronger VPS solve this issue? – Christian Strang Nov 25 '13 at 15:54
  • Can you reproduce the problem at anytime you want? Or did it just happen after lots of minute to produce one? Personally, I think it maybe not relate to your server. You may try it on another blank vps which has no user at all. If it still happen, your vps may has something that block Websocket connection. – Paiboon Panusbordee Nov 26 '13 at 07:17
  • Everytime I stop the script and restart the server it works fine at first, but over time (after 2-3 hours) it starts to fail. After around 5 hours it generally fails (if I reload the site it works around one out of 5 times, often using the long-polling fallback instead of websockets). I will ask the support of digital ocean if they are familiar with the issue :) – Christian Strang Nov 26 '13 at 09:05
  • any other solution ? – Erdi Feb 11 '16 at 08:47
  • @PaiboonPanusbordee where I need to add `transports` on server or client ? – Muhammad Shaharyar Apr 22 '17 at 15:10
  • @MuhammadShaharyar Server – Paiboon Panusbordee Apr 23 '17 at 05:38