1

I start the server and the clients. Then I close the server, and after few minutes reopen it. I get too many messages (from the client). How do I reset it, to start clean.

The server code:

var io = require('socket.io').listen(3001);
var games=[];
 
console.log('start1 server.js');

setTimeout(clear_games,30000);

io.sockets.on('connection', function(socket)
{  
Shlomo
  • 357
  • 3
  • 11

2 Answers2

1

When your active socket.io clients lose the connection to the server, they will constantly try to reconnect until your server comes back online and they successfully get a reconnection. This is the normal and expected behavior and is generally a very useful features since the client will then automatically restablish a connect after a temporary server restart or interrupted network connection.

If you just want to start over for testing purposes, then close all the client web pages, shut down your server, then restart your server, then open the client web pages again. Because the client web pages will not be open when you start your server, it should not immediately get a bunch of reconnects.

If you never want the client to automatically reconnect, then you can specify that in an option in the client code that connects to the server with an option of {reconnection: false}, (though that is generally a bad idea because now the client won't auto-recover if the connection is temporarily interrupted).

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I don't understand. Let say I have 10000 clients working with a server, each sending one message every 10 seconds. If the server is down for 10 minutes, it will need to process 600,000 messages when it restart again. It is unrealistic. I want to avoid it. How do I do it? – Shlomo Mar 29 '16 at 06:44
  • @Shlomo - That is how socket.io is designed. It will buffer messages that you send while the server is down and then send them upon reconnect. You will have to add code to prevent that if that is not the behavior you want. I'd have to know more about the desired behavior when the server goes down and clients are sending to know what to suggest. – jfriend00 Mar 29 '16 at 07:00
  • @Shlomo - The accepted answer to this previous question [SocketIO stop re-emitting event after x seconds/first failed attempt to get a response](http://stackoverflow.com/questions/32131629/socketio-stop-re-emitting-event-after-x-seconds-first-failed-attempt-to-get-a-re/32261523#32261523) sheds some light on a possible solution (clearing buffered data upon reconnect so it is not sent), but I'd need to know exactly what you want the behavior to be before knowing exactly what you need. – jfriend00 Mar 29 '16 at 07:03
  • I read the link you sent me. The question is exactly what I ask, I am surprised that he is the only one that encountered this problem, it should happen to every one. I couldn't understand the answers, my problem is very simple, I get so many messages, that just to ignore them takes a lot of time. Why isn't there a simple function to reset the socket? (ignore). I know that there is an option "forceNew", but all the examples show how to use it in the client, and not in the server. – Shlomo Mar 29 '16 at 11:50
  • @Shlomo - So, does that other answer solve your problem? Can you just add the simple three line `socket.on('connect', ...)` handler to your client code and be done? Or is there more to do here? I can't speak to why socket.io is the way it is - I didn't design it or work on it. It's free, we take it as it is and mold it to fit out needs. – jfriend00 Mar 29 '16 at 16:40
  • I talked to a friend of mine, and he solved the problem. The problem was not that the server accumulated the messages, the problem was that the client kept sending messages, and they were queued in the client side. Once the serve was up, all the clients sent all their accumulated messages. – Shlomo Mar 29 '16 at 17:00
  • @Shlomo - Yes, that three line `socket.on('connect', ...) ` goes in the client to clear the buffer when it reconnects to it won't send any queued messages. That's what that other answer showed. – jfriend00 Mar 29 '16 at 17:15
-1

What I did is: In the client, I added a monitor function that is invoked (setTimeout) every second, and checks the socket.connected. If it is not connected, I don't send any messages to the server till I see that it is true (connected) again. This solved the problem.

Shlomo
  • 357
  • 3
  • 11
  • This is really not a very solid solution as there is plenty of race condition between the time your timer runs and when the connection might actually be marked as not connected any more and when you might send. If you're going to do it this way, then you should just check if the socket is connected EVERY time you want to send data on it. No need for the timer at all. – jfriend00 Apr 01 '16 at 00:07
  • Good point. I will add it, but I stil need the timer, just to show the error for the user. – Shlomo Apr 02 '16 at 00:19
  • You can just use messages to see when the connection is lost and reestablished. No need for polling the flag. – jfriend00 Apr 02 '16 at 00:31