question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug when socket.io connects before first HTTP connection

See original GitHub issue

Hi, I discovered a bug (or maybe just a weird unexpected behavior) with this module.

Description:

When socket.io connects before express.js gets one HTTP request, socket.io gets a new session ID every time it reconnects. As soon as one HTTP request reaches the server, the session ID is static. This leads to unexpected behavior when a socket reconnects after a server restart or network problem.

Code to reproduce:

// bug.js
var app = require('express')(),
    server  = require("http").createServer(app),
    io = require("socket.io")(server),
    session = require("express-session")({
        secret: "my-secret",
        resave: true,
        saveUninitialized: true
    }),
    sharedsession = require("express-socket.io-session");

// Attach session
app.use(session);

app.get('/', function (req, res) {
    console.log('express id: ' + req.session.id);
    res.sendFile(__dirname + '/bug.html');
});

// Share session with io sockets
io.use(sharedsession(session));

io.on("connection", function(socket) {
    console.log('socket.io id: ' + socket.handshake.session.id);        
});

server.listen(3000);

<!--bug.html-->
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="/socket.io/socket.io.js"></script>
    <script>
        var socket = io('http://localhost:3000', {
            reconnection: false
        });
        function httpGet() {
            var xmlHttp = new XMLHttpRequest();
            xmlHttp.open( "GET", 'http://localhost:3000', false );
            xmlHttp.send( null );
            return xmlHttp.responseText;
        }
    </script>
</head>
<body>
    <button onclick="socket.disconnect();">disconnect</button>
    <button onclick="socket.connect();">connect</button>
    <button onclick="httpGet();">http</button>
</body>
</html>

Steps to reproduce:

  1. node bug
  2. Load http://localhost:3000 in browser
  3. ^C (close node)
  4. node bug
  5. Click on connect
  6. Click on HTTP
  7. Click on disconnect
  8. Click on connect

Example Log Output using the above steps:

>node bug
express id: eesau4KnEaNBu9dxc-sd-AsWxOC2pkmx
socket.io id: eesau4KnEaNBu9dxc-sd-AsWxOC2pkmx
^C
>node bug
socket.io id: unGp5Xct-q0sYJQanm5G4R40kBOad6do
express id: N8eXurfyMeqMsIJDf5azw7ZYCwnJkzuy
socket.io id: N8eXurfyMeqMsIJDf5azw7ZYCwnJkzuy

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:20 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
wywzxxzcommented, Sep 11, 2016

Weird things happened. when using var socket = io('http://localhost:3000') or var socket =io('/') in the client html ,sock.io session id is differing from express and will change after refresh. however when using var socket=io() suddenly everything is right in place

1reaction
isleicommented, Aug 7, 2018

I was having the same issue. I had to send a dummy ajax request to the express server to trigger the session creation in the browser. Then I run the socket = io(server_endpoint); in the ajax callback . It seems to be all working fine.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting connection issues | Socket.IO
First and foremost, please note that disconnections are common and expected, even on a stable Internet connection:
Read more >
socket.io server keeps getting connection event but client side ...
The connection event is fired every few seconds on server but my client refuses to connect. When I stop running the client code,...
Read more >
Net | Node.js v19.3.0 Documentation
The node:net module provides an asynchronous network API for creating ... createConnection() , server.listen() , and socket.connect() take a path parameter ...
Read more >
Socket.io - npm
For this purpose, it relies on Engine.IO, which first establishes a long-polling connection, then tries to upgrade to better transports that ...
Read more >
How To Create a Real-Time App with Socket.IO, Angular, and ...
Server(app); const io = require('socket.io')(http); ... The first example we see is when a client connects to the socket server ( connection ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found