How To Create Secure WebSocket With Node - JS
How To Create Secure WebSocket With Node - JS
How To Create Secure WebSocket With Node - JS
JS
"ws" Module
Written on: Sat, 19 Aug 2017
by zackad
Requirements
nodejs with npm
certbot to generate ssl certificate from letsencrypt
After certbot successfully installed, we can generate ssl certificate with command
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.
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/ .
cd
mkdir secure-websocket
cd secure-websocket
npm init
# Fill all the necessary information
All dependencies is ready, now we need to copy our ssl certificate so the application can
access it without superuser privileges.
mkdir ssl-cert
After ssl certificate is ready we can write our application code. Create a new file named
index.js and copy this code.
var fs = require('fs');
httpsServer.listen(8443);
server: httpsServer
});
});
ws.send('something');
});
nodejs index.js
We can test it with this chrome extension or other client to access wss://example.com:8443.
SSL
TUTORIAL
WEBSOCKET