ERR_CERT_INVALID: When trying to connect client using ngx-socket-io to nodejs socketio using HTTPS self-assigned certificate

Viewed 519

I'm trying to connect my socket.io nodejs server to my Angular 9 client. It worked as I was developing my webapp using http, but for production I need to change the connection to be HTTPS, but I'm getting this error:

WebSocket connection to 'wss://localhost:4444/socket.io/?EIO=3&transport=websocket' failed: Error in connection establishment: net::ERR_CERT_INVALID

This is how I initialise the nodeJS server:

var fs = require('fs');
var https = require('https');

var express = require('express');
var app = express();

var options = {
    key: fs.readFileSync('./src/file.pem'),
    cert: fs.readFileSync('./src/file.crt')
};

var httpsServer = https.createServer(options, app);
var io = require('socket.io')(https);

io.on("connection", socket => {
...
});

and I'm connecting my Angular webapp like this:

const config: SocketIoConfig = { url: 'https://localhost:4444', options: { transports: ['websocket'], rejectUnauthorized: false } };
...
imports: [
     SocketIoModule.forRoot(config),
     ...
]

I know the rejectUnauthorized will allow MITM attacks but i just can't figure out whats not working.

Any help would be very appreciated!

1 Answers

It seems your using a private key format that is not supported by socket.io! It should be a '.key' file.

This is my usual setup when I'm using ssl:

const key = fs.readFileSync('privateKey.key');
const cert = fs.readFileSync('certificate.crt');
const ssl = { key: privateKey, cert: certificate };
const app = require('express')();
const https = require('https');
const server = https.createServer(ssl, app);

Hope that helps! ✌️

Michelle

Related