Ws: Error when using a websocket server on SSL (used to broadcast)

Created on 14 Mar 2018  路  2Comments  路  Source: websockets/ws

  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Description

Hello,

I have a websocket server litening on port 4321 that is using Let's encrypt certificates to broadcast every messages it receives.
When my client connects to the server on this way:

ws://example.org:4321

it runs, but when I'm using this syntax:

wss://example.org:4321

I try to use WSS instead of WS
I have this error message:

{ Error: write EPROTO 101057795:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:openssl\ssl\s23_clnt.c:827:

    at _errnoException (util.js:1003:13)
    at WriteWrap.afterWrite [as oncomplete] (net.js:852:14) errno: 'EPROTO', code: 'EPROTO', syscall: 'write' }

Reproducible in:

version: 5.0.0
Node.js version(s): 9.6.1
OS version(s): Windows 10 on my computer, Ubuntu 16.04 on the server

Steps to reproduce:

Here is the code of the server:

const https = require('https');
const fs = require('fs');
const WebSocket = require('ws');

const server = https.createServer({
  cert: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/fullchain.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/privkey.pem')
});

const serveur = new WebSocket.Server(
  {
    server: server,
    port: 4321
  }
);

serveur.on('connection', function connection(ws) {
  console.log('connection');
  ws.on('message', function incoming(data) {
    // Broadcast to everyone else.
    serveur.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  });
});

And the client:

const WebSocket = require('ws');
const ws = new WebSocket('wss://example.org:4321');
var cpt = 0;

ws.on('open', function open() {
    var inter = setInterval(() => {
        cpt++;
        var message = 'test message ' + cpt;
        console.log("Client send: " + message);
        ws.send(message);
    }, 1500);
});

Most helpful comment

If you pass the port option to the WebSocket.Server constructor an internal plain HTTP server is created and started on the specified port.

Don't add the port option and instead call server.listen() on the created HTTPS server:

const server = https.createServer({
  cert: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/fullchain.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/privkey.pem')
});

const wss = new WebSocket.Server({ server });

server.listen(4321);

All 2 comments

If you pass the port option to the WebSocket.Server constructor an internal plain HTTP server is created and started on the specified port.

Don't add the port option and instead call server.listen() on the created HTTPS server:

const server = https.createServer({
  cert: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/fullchain.pem'),
  key: fs.readFileSync('/etc/letsencrypt/live/xxx.xxx/privkey.pem')
});

const wss = new WebSocket.Server({ server });

server.listen(4321);

It solved the problem!
Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jorenvandeweyer picture jorenvandeweyer  路  4Comments

Globik picture Globik  路  3Comments

HanHtoonAung picture HanHtoonAung  路  3Comments

NodePing picture NodePing  路  5Comments

anonaka picture anonaka  路  5Comments