Correct way to restart a socket.io server

11,725

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)
Share:
11,725
blented
Author by

blented

Updated on June 14, 2022

Comments

  • blented
    blented almost 2 years

    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)