23

I am unable to figure out why disconnecting / connecting a socket.io connection multiple times is not working ?

Server side code:

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});

Client side code:

var socket = io();
socket.on('connect', function() {
    console.log("connected from the client side");
});
$('#connect_button').click(function(){
    socket.connect();
});
$('#disconnect_button').click(function(){
    socket.disconnect();
});

It disconnects alright. But doesn't reconnect. I'm using Socket.io 1.0. Please help.

Kaya Toast
  • 5,267
  • 8
  • 35
  • 59
  • Your example works for me. – vinayr Jul 04 '14 at 06:23
  • 1
    it works once. but does it work many times ? It doesn't for me. I was guessing the socket.id changes and then the disconnect is no longer associated with the socket.id of the new connect. Just a guess :-) – Kaya Toast Jul 04 '14 at 06:43

2 Answers2

40

Have you tried this config in client ?

// 0.9  socket.io version
io.connect(SERVER_IP, {'force new connection': true});

// 1.0 socket.io version
io.connect(SERVER_IP, {'forceNew': true});
thomas
  • 2,580
  • 1
  • 22
  • 28
Jujuleder
  • 986
  • 7
  • 12
  • 2
    I tried again, and now it works. I'll post the working code - it may help others. – Kaya Toast Jul 07 '14 at 16:24
  • Thanks @jujuleder! Do you know where the documentation for these io.connect configs are? I couldn't find it anywhere. – ksloan Aug 28 '14 at 01:04
  • 1
    Also of note, you don't need to specify the SERVER_IP. `io.connect({'forceNew':true});` will simply default to using the root url. – Tony Chiboucas Apr 12 '15 at 08:29
  • thank you so much for this answer, it was exactly what I was looking for! for christ's sake I spent like 3 hours reading my angular services / factories / scopes trying to figure out what I was doing wrong. I'm not used to socket.io's style of documentation. – trickpatty Jul 19 '15 at 11:43
23

This solution, based on Jujuleder's answer works. Apparently in socket.io 1.0, it is "forceNew" instead of "force new connection" - both work though.

Server:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
    res.sendfile('s.html');
});

io.on('connection', function(socket){
    console.log('a user connected: ' + socket.id);
    socket.on('disconnect', function(){
        console.log( socket.name + ' has disconnected from the chat.' + socket.id);
    });
    socket.on('join', function (name) {
        socket.name = name;
        console.log(socket.name + ' joined the chat.');
    });
});

http.listen(3000, function(){
    console.log('listening on *:3000');
});

Client (s.html):

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>button{width: 100px;}input{width: 300px;}</style>
</head>
<body>

<ul id="messages"></ul>

<button id="disconnect">disconnect</button>
<button id="connect">connect</button>

<script src="/socket.io/socket.io.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>

    var socket = io.connect('http://localhost:3000');

    $('#disconnect').click(function(){
        socket.disconnect();
    });
    $('#connect').click(function(){
//        socket.socket.reconnect();
//        socket = io.connect('http://localhost:3000',{'force new connection':true });
        socket = io.connect('http://localhost:3000',{'forceNew':true });
        socket.on('connect', function(msg){
            socket.emit('join', prompt('your name?'));
        });
    });
    socket.on('connect', function(msg){
        socket.emit('join', prompt('your name?'));
    });
    socket.on("disconnect", function(){
        console.log("client disconnected from server");
    });

</script>
</body>
</html>
Kaya Toast
  • 5,267
  • 8
  • 35
  • 59