Self-signed certs with secure WebSockets in Node.js

First generate your self-signed certs:

Then create your httpsServer from an express app using node’s built-in https server:

Then setup your websocket server (ironically this will use the same port as the http server, I didn’t know this but I guess protocols can share ports? — this had me going for awhile).

Now browse to server and accept the self-signed cert in Chrome. Then websockets should now work isnide the browser.

Open a chrome devtools console and type:

….or whatever host:port you used for httpsServer, the key here is you’re using wss:// protocol

In your node express web server you should see a message logged to the console. Start the server with node ./server.js

  • Pingback: node.js - Cómo Crear Segura(TLS/SSL) Servidor Websocket()

  • Pingback: Creating Self Signed SSL Certificate using OpenSSL UI – program faq()

  • Konstantin Smirnov

    Hi! Could you help me with timezone change in NPM JS? I want set it to the date() string.

  • Nikk Wong

    thanks so much.

  • Fabio Pistocchi

    Hello. I’m Fabio from Italy. I have been trying for several days to ralizzare a WebSocket Server on my website: I wanted to ask an opinion. The WebSocket server side, you can run on a hosting site like mine or do I purchase a Virtual Private Server?

    • chovysblog

      If you have access to run a web server then it should be fine. The best way is to get a VPS that you have root on. I no longer use shared hosting plans.

  • Henry Umbo CV

    Hi Chovy,

    thanks for this wss: tutorial, but I’m struggling to get it to work as

    while running that node.js server,

    I get this error from the Chrome browser debug console:

    > var ws = new WebSocket(‘wss://’);

    < undefined

    x WebSocket connection to 'wss://' failed: WebSocket opening handshake was canceled

    The Chrome version I'm using is 42.0.2311.135.

    The firefox (version 37.0.2) returns undefined as well.


    • Ahmed Sabry

      you can fix this problem via:
      From you browser (eg. chrome) go to you will get an warning “Your connection is’t private” -> click “Advanced” button Proceed to (unsafe) and now you can try wss without problems.