Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
WebSocket server tied to the HTTP server but why?

I found a lot of examples that shows how to create a websocket server (nodejs).

Example for illustration (on my side I am using expressJS):

var http = require('http'); /** QUESTION 2 (optional) */
var WebSocketServer = require('websocket').server;


var httpServer = http.createServer((request, response) => {
  ...
});
httpServer.listen(8080, () => {
  ...
});

wsServer = new WebSocketServer({
    httpServer: server, /** QUESTION 1 */
    ...
});

QUESTION 1: Why does the WebSocketServer needs the httpServer instance ? I would imagined creating a WS Server specifying a host and port only, not a HTTP instance.

QUESTION 2: Can I use ExpressJS ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Finally got it !

So HTTP and WebSockets are both protocols. WebSockets uses HTTP protocol to work. So answering my own QUESTION 1: WebSockets increases the HTTP protocol by knowing how to communicate with websocket protocol. This is why a HTTP server is needed.

For question 2, it's a bit more subtile. You can use two differents HTTP instance (one for a normal HTTP API and another one for the WebSockets). Then it brings the question of: Why do you need both ? And the answer is pretty logical. Well if you already have a HTTP instance (http.createServer or Express appServer), then you can re-use the same HTTP instance to avoid being greedy on your server resources.

So basically, to use the same HTTP instance in Express and with websocket.io (as an illustration). You would do

const express = require('express');
const app = express();
const http = require('http');
const server = http.createServer(app);
const { Server } = require("socket.io");

/** socket.io is using the single 'server' instance (bound to it) */
const io = new Server(server);

/** Add a route to express that is bound to the single HTTP server instance */
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html');
});

/** Add a websocket path that is bound to the single HTTP server instance */
io.on('connection', (socket) => {
  console.log('a user connected');
});

/** Express is using the single 'server' instance (bound to it). */
server.listen(3000, () => {
  console.log('listening on *:3000');
});

Source: https://socket.io/fr/get-started/chat

In that example we can see that expressJS and websocket.io are using the same HTTP instance ! \o/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!