Bug when socket.io connects before first HTTP connection
See original GitHub issueHi, 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:
node bug
- Load
http://localhost:3000
in browser - ^C (close node)
node bug
- Click on connect
- Click on HTTP
- Click on disconnect
- 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:
- Created 8 years ago
- Reactions:2
- Comments:20 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Weird things happened. when using
var socket = io('http://localhost:3000')
orvar socket =io('/')
in the client html ,sock.io session id is differing from express and will change after refresh. however when usingvar socket=io()
suddenly everything is right in placeI 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.