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 https://0.0.0.0:8443 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

  • Konstantin Smirnov

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

  • 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: http://www.sition.it

    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://0.0.0.0:8443’);

    < undefined

    x WebSocket connection to 'wss://0.0.0.0:8443/' 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.

    TsongHong

    • Ahmed Sabry

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