Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I setup the einaros/ws module for NodeJS to accept wss (secure ws) connections?

I am using the eniaros/ws module for NodeJS to communicate in real time between an iOS app and the NodeJS server using web sockets.

The app runs fine, but now I have to secure the communications between the server and iOS app using wss. I have my Rapid SSL certificate and key and I am using the following code on the server:

var fs = require('fs');

var cfg = {
    ssl: true,
    port: 8081,
    ssl_key: 'key.key',
    ssl_cert: 'cert.crt'
};

var httpServ = ( cfg.ssl ) ? require('https') : require('http');

var app      = null;

// dummy request processing
var processRequest = function( req, res ) {

    res.writeHead(200);
    res.end("All glory to WebSockets!\n");
};

if ( cfg.ssl ) {

    app = httpServ.createServer({

        // providing server with  SSL key/cert
        key: fs.readFileSync( cfg.ssl_key ),
        cert: fs.readFileSync( cfg.ssl_cert )

    }, processRequest ).listen( cfg.port );

} else {

    app = httpServ.createServer( processRequest ).listen( cfg.port );
}

// passing or reference to web server so WS would knew port and SSL capabilities
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer( { server: app } );

The code is pretty much the example given in the ws/examples/ssl.js file on GitHub, but for some reason the NodeJS/ws server does not accept any wss socket connections.

I am using the correct path wss://REMOTE_SERVER_IP:8081/ and I'm testing with http://www.websocket.org/echo.html .

I have searched far and wide but there does not seem to be an up to date tutorial on how to setup secure wss using eniaros/ws .

like image 701
Octavian Naicu Avatar asked Jul 02 '14 07:07

Octavian Naicu


1 Answers

3 months later I revisited the problem and solved the issue.

The code is correct.

My error was that I was trying to connect the client using the ip of the server (wss://xx.xx.xx.xx:8081) instead of using the subdomain for which the certificate and key were issued (wss://secure.subdoma.in:8081).

like image 115
Octavian Naicu Avatar answered Oct 05 '22 07:10

Octavian Naicu