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.

doc suggestion: do not use express app.listen() with socket.io

See original GitHub issue

I ran into this in the process of adding socket.io to an existing express(4) app because i didn’t switch the code from app.listen() to server.listen().

The symptom is refusal by the server to serve the client library or connect any sockets.

According to http://expressjs.com/4x/api.html#app.listen , “This method is identical to node’s http.Server.listen()” - but in reality it creates a new http server leaving the one with socket.io non-operational.

Hopefully this issue will help others to avoid the same trap.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
TifPuncommented, Oct 13, 2016

+1

Did a bit more research and found a way (more details here) to stick with app.listen():

var io = require("socket.io").listen(server);
app.set("port", (process.env.PORT || 3000));
var server = app.listen(app.get("port"), function(){
  console.log("Server started: http://localhost:" + app.get("port") + "/");
})

which seems to work the same as:

var io = require("socket.io")(server);
var server = require('http').Server(app);
server.listen((process.env.PORT || 3000), function(){
  console.log("Server started: http://localhost:" + app.get("port") + "/");
});
1reaction
darrachequesnecommented, Feb 17, 2021

This is indeed a common trap… A note was added here: https://socket.io/docs/v3/server-initialization/#With-Express

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use app.listen with Socket.io in Node? - Stack Overflow
If you want to run socket.io on the same port as webserver, you can try this: const app = express(); const server =...
Read more >
Server Initialization | Socket.IO
Once you have installed the Socket.IO server library, you can now init the server. The complete list of options can be found below....
Read more >
Everything you need to know about Socket.IO - Ably Realtime
A popular way to demonstrate the two-way communication Socket.IO provides is a basic chat app (we talk about some other use cases below)....
Read more >
Migrating to Express 4
The http module is no longer needed, unless you need to directly work with it (socket.io/SPDY/HTTPS). The app can be started by using...
Read more >
Node.js Socket.io Complete Tutorial - Codedamn
const express = require('express'); const app = express(); app.get('/', (req, res) => ...
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