What exactly does code something like
var WebSocketServer = require("ws").Server,
express = require("express"),
http = require("http"),
app = express(),
server = http.createServer(app);
var wss = new WebSocketServer({server: server});
mean? What is that really doing - why would you need to give a websocket server an http server?

- 75
- 1
- 6
2 Answers
All webSocket connections start with an http request from the client (that contains an upgrade
header). Once both sides agree that an upgrade to the webSocket protocol is OK and have exchanged some security credentials, then the protocol is upgraded to the webSocket protocol and all future communication on that socket is using the webSocket protocol, not the http protocol.
But, EVERY webSocket server has to be an http server for the webSocket initiation process.
You can choose whether you want this http server to be a shared http server that is also being used for your own http requests or whether you want a separate http server to be created just for the webSocket connections. If you use the separate http server, then it will have to be on a separate port (since you can't have two servers on the same host operating on the same port).
When using the shared http server, there is a small webSocket listener that examines each incoming http request. If that incoming request contains the Upgrade: websocket
header, then it takes over that incoming request. If not, it lets the regular http server logic handle the request as a normal http request. In this manner, the same http server can be used for both http requests and incoming websocket connection requests.
So, by sharing with the http server, everything (both your http requests and your webSocket connections) can operate on the usual default ports, port 80 (for http) or port 443 (for https).
Some relevant references:
Why WebSocket can share the 80 port with HTTP "after the handshake"?
Do websocket implementations use http protocol internally?
What's the difference between WebSocket and plain socket communication?
Examples of establishing a websocket protocol connection over http

- 683,504
- 96
- 985
- 979
-
Thank you for the great answer. If I may ask, what are the differences between SockJS and websockets through something like ws? Is SockJS more than just websockets? – hawexp Jan 12 '20 at 17:59
-
@hawexp - sockJS is a higher level layer that can run on top of webSockets that adds some features (same general concept as socket.io). You can read more about socketJS to see what it offers. I'm personally more familiar with socket.io than sockJS. – jfriend00 Jan 12 '20 at 18:12
That's because a WebSocket connection is often an Upgrade
from a HTTP one. The HTTP server handles the full handshake with the client, then delegates everything else to WebSocket. See Protocol upgrade mechanism for more informations on this.

- 30,447
- 6
- 48
- 86
-
So is it optional to pass the http server instance? What would be the benefit of doing it over not doing it? – hawexp Jan 12 '20 at 17:40
-
According to the documentation here: https://github.com/websockets/ws yes this is optional. I guess the choice depends on whether you need to use HTTP for your service or not at all – Guerric P Jan 12 '20 at 17:48