5

I'm trying to restart a socket.io server. I start the server and get a welcome message for new connections, but when I close and restart the server I get no further welcome message.

Hopefully I'm missing something simple :\

var http = require('http').Server
var socketIO = require('socket.io')
var socketIOClient = require('socket.io-client')
var port   = 3000
var url = 'ws://localhost:' + port

function newServer(serverName, cb)
{
    var server = http().listen(port, function()
    {
        console.log(serverName, 'listening')
        var io = socketIO(server)

        var clientSocket = socketIOClient(url,
            { reconnection: false })

        clientSocket.on('connect', function()
        {
            // never get 'two connect'
            console.log(serverName, 'connect')
            io.close()
        })
        clientSocket.on('disconnect', function()
        {
            console.log(serverName, 'disconnect')
            cb()
        })
    })
}

function startServerOne(cb)
{
    newServer('one', cb)
}
function startServerTwo(cb)
{
    newServer('two', cb)
}

startServerOne(startServerTwo)
blented
  • 2,699
  • 2
  • 23
  • 17

2 Answers2

9

The parameter I was looking for was "forceNew". It's undocumented in socket.io-client documentation.

This seems to force the socket.io-client to create a new manager instead of using the cached one (which I assume is connected to a server that's no longer running).

The option is described on the socket.io blog and can be seen in the code here with a discussion of the issue here

Full working example:

var http = require('http').Server
var socketIO = require('socket.io')
var socketIOClient = require('socket.io-client')
var port   = 3000
var url = 'ws://localhost:' + port

function newServer(serverName, cb)
{
    var server = http().listen(port, function()
    {
        console.log(serverName, 'listening')
        var io = socketIO(server)

        var clientSocket = socketIOClient(url,
            {
                reconnection: false,
                //////////////////////////////
                // this forces a new connection!
                forceNew: true
                //////////////////////////////
            })

        clientSocket.on('connect', function()
        {
            // never get 'two connect'
            console.log(serverName, 'connect')
            io.close()
        })
        clientSocket.on('disconnect', function()
        {
            console.log(serverName, 'disconnect')
            cb()
        })
    })
}

function startServerOne(cb)
{
    newServer('one', cb)
}
function startServerTwo()
{
    newServer('two', function()
    {
        console.log('high five everyone')
    })
}

startServerOne(startServerTwo)
blented
  • 2,699
  • 2
  • 23
  • 17
0

When you restart the server you kill all connections. Clients should actively reconnect.

You should take a look towards auto-reconnect configuration of client sockets

Kirill Slatin
  • 6,085
  • 3
  • 18
  • 38
  • My goal is for the old client not to reconnect, just the new one I create after restarting the server. It seems like the server starts fine but socket.io is missing a "start" method or something as it never comes back. – blented Jul 25 '15 at 21:33
  • anything special in new client's console or on server's? – Kirill Slatin Jul 25 '15 at 22:07
  • 2
    For unit testing. I don't want old clients from previous tests to still be hanging around. Ideally each test would be against a fresh server and io but I can't seem to get that working. Examples in the [socket.io tests](https://github.com/socketio/socket.io/blob/master/test/socket.io.js) don't seem to be doing anything special.. – blented Jul 25 '15 at 22:10