Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

263
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda