Last Updated: February 25, 2016
·
2.261K
· 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.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside