Search code examples
javascriptwebsocket

Error in establishing websocket connection


I am using ws npm in the server side, websocket in the client side.

When running this code from node-js it works fine, but running it from the browser gives the following error:

failed: Error in connection establishment: net::ERR_CERT_COMMON_NAME_INVALID
const ws = new WebSocket('wss://domain:port', null, { 
  rejectUnauthorized: false 
});

ws.onerror = function (e) {
  console.log(e)
}

ws.onclose = function (e) {
  console.log(e)
}

ws.onopen = function () {
  console.log('connected ')
  ws.send(JSON.stringify({ msg: 'msg' }));
}

Solution

  • net::ERR_CERT_COMMON_NAME_INVALID this looks to be a certificate issue. There are various certificates that a browser use. For eg. you can see the certificates in Mozilla Firefox in Preferences > Privacy & Security > Certificates, and you can explore which certificate is causing you the problem.

    Make sure your API is pointing to the correct certificate