0

I see a lot of examples hooking an http server during the creation of a WS server, more or less like the following

var server = http.createServer(function(request, response) {
  // process HTTP request. Since we're writing just WebSockets
  // server we don't have to implement anything.
});
server.listen(1337, function() { });

// create the server
wsServer = new WebSocketServer({
  httpServer: server
});

or

var httpServer =  http.createServer().listen(websocketport);

/*
* Hook websockets in to http server
*/

socketServer.installHandlers(httpServer, { prefix: '/websockets' });

I dont understand the reason why. Is there any benefit from that?

What is wrong with the classic WS setup, like so

const WebSocket = require('ws')

const wss = new WebSocket.Server({ port: 8080 })

wss.on('connection', ws => {....

Why shouldn't I just use a WS server with no http server at all?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
codebot
  • 517
  • 8
  • 29
  • 55
  • 1
    Probably because encapsulating it in a Node.js `http.Server` allows it to be compatible with other frameworks like `express`, whereas you probably would not be able to pass an instance of `ws.Server` directly to `express` – Patrick Roberts Aug 27 '19 at 14:13
  • @PatrickRoberts That is a good observation. Do you have any examples of that? And, by the way, why should you pass a ws to express? Routing? If that is the case, then yes, maybe this is useful. Other than that, I see no reason – codebot Aug 27 '19 at 14:17
  • websocket servers only handle upgrading HTTP connections to websockets, they do not handle any other kinds of requests such as serving HTML documents, other static resources, API requests, etc. – Patrick Roberts Aug 27 '19 at 14:20

2 Answers2

0

This depends entirely on the project.

Typically, most examples you will see are not pure websocket server examples and instead will assume that you are incorporating websocket functionality into a larger application stack.

If you have no need for an HTTP server, then you shouldn't attach your websocket listener to an instance of one. If you do need an HTTP server, then you are best off attaching your websocket listener in the following situations:

  • If your HTTP server and your WebSocket listener are on the same domain
  • You want to be able to manage or push to your socket connections when some HTTP requests are made.
  • Thanks for the answer. Can you please be more specific about the benefits of HTTP in WS? Is there any link with an example that I can see? Thanks – codebot Aug 28 '19 at 07:28
0

This is most likely because usually, you would provide some REST APIs which uses the HTTP server along with your WS server.

If you are trying to use the same host and port for both of them (HTTP & WS) together, you need to sort of combine or encapsulate one of them into another so that it can accept and handle both kinds of protocol.

However, if you are only trying to use WS without HTTP, then you probably do not need the HTTP server, just the WS server will do, as you have shown in your classic WS setup.

On the other hand, if you only need the HTTP server without WS, then you do not need to implement WS, just HTTP server will do.

Yee Hui Poh
  • 366
  • 2
  • 6