Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Socket.io: First Client disconnecting on Second connection

Socket.io: First Client disconnecting on Second connection

Problem

I am creating a Socket.io app. When I create a connection to the socket on firefox, it established just fine. When I am using chrome to init another connection to the socket, the first client gets disconnected and then the two clients reconnect again (with the same socket.id)

This is the code I have been working with:

app.post('/auth', function(req, res){   // routes.auth(hash, db, io, pseudoArray, connections, Session));
    var username = req.body.username,
        password = req.body.password;
    if (username != "" && password != ""){
        authenticate(username, password, db, hash, function(err, user){
            if (user) {
                // Regenerate session when signing in
                // to prevent fixation 
                console.log('user authenticated');
                req.session.regenerate(function(){
                    req.session.user = user.name;
                    req.session.success = 'Authenticated as ' + user.name
                    + ' click to logout. '
                    + ' You may now access /restricted.'; 

                    var Session = connect.middleware.session.Session;
                    io.sockets.on('connection', function (socket) {
                        var hs = socket.handshake;
                        console.log('A socket with sessionID ' + hs.sessionID + ' connected!');

                        socket.set('pseudo', req.session.user, function(){
                            pseudoArray.push(req.session.user);
                            socket.emit('pseudoStatus', req.session.user);
                            console.log("user " + req.session.user + " connected");
                        });

                        socket.on('pseudoOk', function(data){
                        //  connections[data] = socket; 
                            connections[data] = socket.id; // connected user with its socket.id
                            connectedUsers[socket.id] = socket;

                            console.log('----CONNECTIONS----');
                            console.log(connections);
                            console.log('++ USERS ++ ');
                            console.log(connectedUsers);
                            console.log('----END CONNECTIONS----');
                        });

                        socket.on('disconnect', function () {
                            console.log('A socket with sessionID ' + hs.sessionID + ' disconnected!');

                            // clear the socket interval to stop refreshing the session
                            // clearInterval(intervalID);
                        });
                    });

                    res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true });
                    res.redirect('home');
                });
            } else {
                console.log('auth failed');
                req.session.error = 'Authentication failed, please check your '
                    + ' username and password.'
                    + ' (use "tj" and "foobar")';
                res.redirect('login');
            }
        }); 
    } else {
        res.redirect('connect');
    }
});

Any clues?

The main question is: Will a socket connection on a single computer (localhost) work within two different browsers on the same machine? What I am thinking is that since I am using two browsers on a single machine, I am getting the same socket id for both the browsers. Is that the case?

Problem courtesy of: amit

Solution

You're creating an event listener on the socket.io connection event for every POST request to /auth. That's going to result in undefined behaviour and possibly memory leaks. Express and socket.io are two separate subsystems running within the same server application. As such, they shouldn't be mixed like that.

A 'normal' Express/socket.io setup looks like this:

// your Express routes
app.post('/auth', function(req, res) { ... });
...

// socket.io setup (completely separate)
io.sockets.on('connection', function(socket) {
  ...
});

If you want to share session objects between Express and socket.io: that isn't trivial. Like I said before, both systems should be considered separate from each other. However, there have been some clever people that got it to work. I don't know how up-to-date that blogpost is, though, and it requires quite in-depth knowledge of both Express and socket.io.

Solution courtesy of: robertklep

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Socket.io: First Client disconnecting on Second connection

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×