HenningM / express-ws

WebSocket endpoints for express applications
BSD 2-Clause "Simplified" License
877 stars 142 forks source link

WSS support #155

Open loretoparisi opened 2 years ago

loretoparisi commented 2 years ago

I have a security error in Chrome

app.js:271 Mixed Content: The page at 'https://mywebsite' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://mywebsite/ss/1.0/echo'. This request has been blocked; this endpoint must be available over WSS.

When I switch to wss:// protocol, I have a connection error (I assume):

VM342:1 WebSocket connection to 'wss://mywebsite/ss/1.0/echo' failed: 

the WebSocket object state is like

WebSocket {url: 'wss://mywebsite/ss/1.0/echo', readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, …}
binaryType: "blob"
bufferedAmount: 0
extensions: ""
onclose: null
onerror: null
onmessage: null
onopen: null
protocol: ""
readyState: 3
url: "wss://mywebsite/ss/1.0/echo"

In the server I have


self.ws = expressWs(self.app, self.httpServer.getInstance(), {
    port: 3000,
    perMessageDeflate: false
});

self.app.ws(self._options.wsbaseUrl + '/echo', function(ws, req) {
                ws.on('message', function(msg) {
                    try {
                        const jsonObj = JSON.parse(msg);
                        const jsonString = JSON.stringify(jsonObj);
                        ws.send(jsonString);
                    } catch(error) {
                        self.logger.warn("websocket error:%s", error.toString());
                    }
                });
            });

where

self.app=express()
self.httpServer.getInstance() is the nodejs http server instance

The server is running on port 3000, while a load balancer sends traffic from the 80 over https to the port 3000 over http.

ghost commented 2 years ago

A website WITH SSL cannot connect to a websocket WITHOUT SSL.

matthijs166 commented 1 year ago

There is an example available here to add HTTPS: https://github.com/HenningM/express-ws/blob/master/examples/https.js