mlewis54 mlewis54 - 2 years ago 75
Javascript Question

Websockets over WSS:// doesn't appear to fire events on server

I am trying to set up a basic WSS websockets server. This is my minimal HTML (with the embedded javascript):

<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<body style="background-color:white">
<h1>Test of WSS server</h1>
<p>Status: <span id=status"></span></p>
<a href="javascript:message();">Click to send message</a>
<script src="/newjs/jquery-2.1.1.js"></script>
var connection;
$(document).ready(function () {
window.WebSocket = window.WebSocket || window.MozWebSocket;
if (!window.WebSocket) {
alert("browser says no");
console.log("Browser does not supports websockets");
function message() {
var msg = "Test Message";
function setupConnection() {
connection = new WebSocket('wss://');
connection.onerror = function(error) {
console.log('onerror fired');
connection.onopen = function(event) {
connection.onmessage = function (message) {
setInterval(function() {
if (connection.readyState !== 1) {
}, 5000);

The following is the JS server run by nodejs:

var fs=require("fs");
var ws_cfg = {
ssl: true,
port: 14000,
ssl_key: '/httpd/conf/ssl.key/my.key',
ssl_cert: '/httpd/conf/ssl.crt/my.crt',
ca_cert: '/httpd/conf/ssl.crt/gd_bundle-g2-g1.crt'
var processRequest = function(req, res) {
console.log("Request received.")
var httpServ = require('https');
var app = null;
app = httpServ.createServer({
key: fs.readFileSync(ws_cfg.ssl_key),
cert: fs.readFileSync(ws_cfg.ssl_cert),
ca: fs.readFileSync(ws_cfg.ca_cert),
var WebSocketServer = require('ws').Server, ws_server = new WebSocketServer( {server: app});

ws_server.on('open',function(request) {
ws_server.on('request', function(request) {
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
if (request.origin!='') {
console.log("rejecting request from " + request.origin + " as not coming from our web site");
var connection = request.accept(null, request.origin);
connection.on('message', function(message) {
console.log("Got a message");

I fire up the server with node then load the web page in my browser (using either FF or Chrome). Using the developer tools I see that the connection appears to be made. On the server side I see the established connection using netstat. I also put an alert() in the browser side in the onopen() function and it fired.

The problem is that no console log output is produced. When connection.send(mag) is executed the on("message" event never appears to fire on the server. I'm at a loss here. I had this working as an http:// websocket server but this is my first attempt at wss:. I would appreciate any insight.

The sever name is not although that is what I show in my code.

The firewall is allowing anyone to connect on port 14000 using TCP protocol.

The cert is a working wildcard cert for the web site.

Answer Source

Finally figured out what it was after ignoring it for a month or so. It had to do with the symbolic link (/httpd) defined for the SSL files as in:

ssl_key: '/httpd/conf/ssl.key/my.key',
ssl_cert: '/httpd/conf/ssl.crt/my.crt',

They had to be changed to:

ssl_key: '/usr/local/apache2/conf/ssl.key/my.key',
ssl_cert: '/usr/local/apache2/conf/ssl.crt/my.crt',

Who knew that symbolic links were frowned upon? Well, now we all do.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download