Question

In my node server.js file I'm setting socket.io with:

var io = require('socket.io').listen(socketPort);
io.sockets.on('connection', function (socket) {
   console.log(socket.id);
});

So this works ok when a client connects, but when I restart my node server it calls this function again with all clients previously connected. I.e., something caches the clients. But I don't want it to do that. I'd like it to start the server from fresh. And only calls this event once per client connection (when client starts his browser, or refreshes browser).

Or atleast, I'd like a flag to know that this is a connection that happened after I restarted my server. Any suggestions?

Maybe there's a way I can flush out the sockets when my server restarts?

I saw this thread: socket.io creates one more connection after reconnecting But I don't know where "iosocket.once('connect', function () { })" comes from since I'm using io.sockets

Was it helpful?

Solution

Your clients are trying to reconnect automatically.

Do this on your client, and they won't automatically try to reconnect:

var socket = io.connect('http://server.com', {
  reconnect:false
});

You can see more client options here: https://github.com/socketio/socket.io-client/blob/master/docs/API.md#new-managerurl-options

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top