0% found this document useful (0 votes)
40 views3 pages

How To Create Secure WebSocket With Node - JS

This document provides steps to create a secure WebSocket server with Node.js using the "ws" module and SSL certificates from Let's Encrypt. It describes generating SSL certificates with Certbot, setting up a Node.js project with the ws, fs and https modules, copying the SSL certificates to the project directory, and providing sample server code to create an HTTPS server and WebSocket server using the SSL certificates.

Uploaded by

The God Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

How To Create Secure WebSocket With Node - JS

This document provides steps to create a secure WebSocket server with Node.js using the "ws" module and SSL certificates from Let's Encrypt. It describes generating SSL certificates with Certbot, setting up a Node.js project with the ws, fs and https modules, copying the SSL certificates to the project directory, and providing sample server code to create an HTTPS server and WebSocket server using the SSL certificates.

Uploaded by

The God Man
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

How To Create Secure WebSocket With Node.

JS
"ws" Module
Written on: Sat, 19 Aug 2017
by zackad

In this tutorial, we will create ssl enable websocket using ws module.

Requirements
nodejs with npm
certbot to generate ssl certificate from letsencrypt

Step 1 — Generating SSL Certificate


Assuming you use ubuntu 16.04 the step are following

sudo add-apt-repository ppa:certbot/certbot

sudo apt-get update

sudo apt-get install certbot

If you’re using different system, please refer to this official documentation.

After certbot successfully installed, we can generate ssl certificate with command

sudo certbot certonly --webroot -w /var/www/html -d example.com -d www.domain.com

Otherwise, if we don’t want to use webroot plugin we can use –standalone flag to generate
ssl certificate.

Please make sure that port 443 is not being used when generating ssl
certificate using standalone mode. We can temporarily stopping
webserver/other process for using port 443 and start again webserver
when we done.

sudo certbot certonly --standalone -d example.com -d www.example.com

This command will generate a single certificate for domain example.com and
www.example.com. Now we can copy certificate file located in (usually)
/etc/letsencrypt/live/example.com/ .

Step 2 — Create WebSocket Server Project


For now, lets create a project name secure-websocket in our home directory and initiating
nodejs project.

cd

mkdir secure-websocket

cd secure-websocket

npm init
# Fill all the necessary information

npm install ws --save

npm install fs --save

npm install https --save

All dependencies is ready, now we need to copy our ssl certificate so the application can
access it without superuser privileges.

# create directory to contain ssl certificate

mkdir ssl-cert

# copy ssl certificate to our project directory

sudo cp /etc/letsencrypt/live/example.com/fullchain.pem ssl-celt/fullchain.pem

sudo cp /etc/letsencrypt/live/example.com/privkey.pem ssl-celt/privkey.pem

After ssl certificate is ready we can write our application code. Create a new file named
index.js and copy this code.

// Minimal amount of secure websocket server

var fs = require('fs');

// read ssl certificate

var privateKey = fs.readFileSync('ssl-cert/privkey.pem', 'utf8');

var certificate = fs.readFileSync('ssl-cert/fullchain.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');

});

To start websocket server run following command

nodejs index.js

We can test it with this chrome extension or other client to access wss://example.com:8443.

SSL
TUTORIAL
WEBSOCKET

Contact I'm a simple man. I do what I want and what I


zackad [dot] dev [at] gmail [dot] com could. Otherwise it's a trial and error.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy