Get Even More Visitors To Your Blog, Upgrade To A Business Listing >> First Client disconnecting on Second connection First Client disconnecting on Second connection


I am creating a 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

This is the code I have been working with:'/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.user =;
                    req.session.success = 'Authenticated as ' +
                    + ' 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(){
                            socket.emit('pseudoStatus', req.session.user);
                            console.log("user " + req.session.user + " connected");

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

                            console.log('++ USERS ++ ');
                            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 });
            } else {
                console.log('auth failed');
                req.session.error = 'Authentication failed, please check your '
                    + ' username and password.'
                    + ' (use "tj" and "foobar")';
    } else {

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


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

A 'normal' Express/ setup looks like this:

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

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

If you want to share session objects between Express and 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

Solution courtesy of: robertklep


View additional discussion.

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

Share the post First Client disconnecting on Second connection


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription