I would like to learn about secure webssockets, so i tried to follow the tutorial in https://blog.zackad.com/en/2017/08/19/create-websocket-with-nodejs.html
I generated a SSL certificate with certbot (Let's Encrypt) and copied the private key and certificate files into my project.
Unfortunately the following program failed, obviously in reading with the shown errors. What could be possibly went wrong?
// Minimal amount of secure websocket server
var fs = require('fs');
// read ssl certificate
var privateKey = fs.readFileSync('0000_key-certbot.pem', 'utf8');
var certificate = fs.readFileSync('0000_csr-certbot.pem', 'utf8');
var credentials = { key: privateKey, cert: certificate };
var https = require('https');
//pass in your credentials to create an https server
var httpsServer = https.createServer(credentials);
httpsServer.listen(8443);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({
server: httpsServer
});
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
ws.send('reply from server : ' + message)
});
ws.send('something');
});
Error:
_tls_common.js:88
c.context.setCert(options.cert);
Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
at Object.createSecureContext (_tls_common.js:88:17)
at Server (_tls_wrap.js:803:25)
at new Server (https.js:54:14)
at Object.createServer (https.js:76:10)
at Object.<anonymous> (/Users/me/Desktop/TestHTTPS/test.js:12:25)
at Module._compile (module.js:635:30)
at Object.Module._extensions..js (module.js:646:10)
at Module.load (module.js:554:32)
at tryModuleLoad (module.js:497:12)
at Function.Module._load (module.js:489:3)
My certification file looks like this (the key very similar):
> -----BEGIN CERTIFICATE REQUEST-----
MIICezCCAWMCAQIwADCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAM69
c39TcFkyLrQ1fCsCNFXCaG1ZXzp6JLWw/Sg/KHtHH8vHP2o/nWPjFr0OCsgpAQGS
NVDy28AcZzwkRVbytEguUUKjIFkpzUkbkxqHTzN8S+awQDGtIdaFrKirZjRIJIhK
t+CkMq6w25iD+SBU8TV2GVe+TG9q50Skgt+tDboUwn8oWJs9lMoauE3IaRTzCKAQ
mBJPgoC5PQLKxrtPp0SRwh2mSXByEQNbYIS0lpAmXrgkWGrn3OPsCD8+uBGzJL9E
JhucwjdtEGdyj11Pgbm0MYjmfJjUmGH3aqkcINdxdbWhPQtKzIT76cOq+KOT9vs7
6qts9RHm6R17cLJgk3ECAwEAAaA2MDQGCSqGSIb3DQEJDjEnMCUwIwYDVR0RBBww
GoIHbXlkcy5tZYIPdndhbHRlci5teWRzLm1lMA0GCSqGSIb3DQEBCwUAA4IBAQBc
EHsYLA5rP/zq7Eerj2DWyLieSdzKa3gXn69E1Y9T6nGRFOFAUigxuzWHUZ8OMEQh
Yqb21ms7hRbbxgpTtV7tALLlzhVa+YOcowj/uPMlVLNYAG51eJTTneEWzk0g/XM8
bZf10ueF3Om7Pdn94Dik+ShjCI7WEBoHaC4tBSYzwitYeaPrc1NYHpF5XjbXXp5O
K85KvCIgLqvlXXV59ax4JAgHSzlnR0y2ZxY7Vn4wEwU2RiSzt+aJnZoIPXUc4/aW
7YMVT3UMe9smT2QIIN2enFRf6XvaMmzUOPL2B5nvibSyByfoc5bxGc6DySZZ7v2T
stShlDo9oalYMxbW5ESf
-----END CERTIFICATE REQUEST-----