How to cleanly disconnect from a namespace in socket.io?

I have been trying to disconnect from a namespace or even from the main socket connection itself but have been failing. Below is my code

Server Side:

 socket.on('userDisconnect', function () {        
    socket.disconnect();
    console.log("User Disconnected");       

}); 

Client Side:

// already connected to /world namespace
socket.emit('userDisconnect');
    socket.disconnect();
socket = io.connect('http://something/india' );

I tried disconnecting from both client and serve side but it doesnt work. Can anyone point out the mistake. This is what is been written to console by socket.io

   info  - booting client
 debug - websocket writing 0::/world
User Disconnected
   debug - client authorized for /india
   debug - websocket writing 1::/india

/world is the namespace its trying to disconnect from and then its trying to connect to /india namespace.

This worked for me

broadcastSocket.packet({ type: 'disconnect' });
    broadcastSocket.$emit('disconnect');
    disconnectNamespace(broadcastSocket.name, broadcastSocket.socket);        


function disconnectNamespace (name,socket) {
    if (socket.namespaces[name]) {
        var nsp = socket.of(name);
        nsp.packet({ type: 'disconnect' });
        nsp.$emit('disconnect');
        delete socket.namespaces[name];

        if (Object.keys(socket.namespaces).length === 0) {
            socket.disconnect();
        }
    }
};

It doesn't look like you told the client to wait for disconnect before reconnecting.

// already connected to /world namespace
socket.emit('userDisconnect');
socket.on('disconnect', function () {
  socket = io.connect('http://something/india');
  // stuff with india socket
});

For socket.io v.1.3.2

Create with:

sio = io('ws://localhost:13000/device');

Delete with:

sio.disconnect();
delete sio.io.nsps[sio.nsp]; // sio.nsp = '/device'
delete sio;

This worked for me.

Use disconnect() method on the server or on the client (or both like your did in your example). When you first connect to the server, add 'forceNew':false to the client connect method, something like this:

var socket = io('http://localhost:3001', {'forceNew':false});