Last Updated: February 25, 2016
·
2.052K
· mkaminsky11

Securing Websockets

Websockets is a great tool for client-to-server communication via Node.js . However, as you shouldn't send data unsecured.

On the client side:

var connection = new WebSocket('wss://example.com:8080');

The "wss" is really important. That tells the client the connection is secure. On the server side:

var webSocketServer = require('websocket').server;
var https = require('https');

var options = {
    key: fs.readFileSync(".your_key.pem"),
    cert: fs.readFileSync("your_cert.pem"),
    ca: [fs.readFileSync("your_ca.pem")]
};

var server = https.createServer(options, function(req, res){
});

And then continue as you would normally.