camillo777
camillo777

Reputation: 2367

WebSockets on OpenShift do not work with remote client

I have an issue that I cannot solve. I implemented a node js WebSockets server on an openshift cartridge using socket.io or WebSockets node js libraries. With any of them the result is the same.

With a node js client running on the same openshift platform everything works ok.

When the client is moved on my local pc the client connects and suddenly disconnects giving a 1011 internal server error.

I tried using other well known clients like the echo service on WebSockets.Org or jsfiddle but the result is the same, connect and suddenly disconnect.

Do not know why. Have someone been able to connect to an openshift WebSockets server from remote? The server is minimal and simple, and locally it works ok.

This is the code I am using as the server:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port      = 8000;

var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);

var handleClient = function (socket) {
    // we've got a client connection
    socket.sendUTF("hello");
    console.log("connect");
};

io.on("connection", handleClient);

server.listen(port, ipaddress);

And this is the socket endpoint: ws://js-camillorh.rhcloud.com:8000

Thank you!

Updated the code to this after comments:

var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "127.0.0.1";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var app = require("express");
var server = require("http").Server(app);
var io = require("socket.io")(server);

var handleClient = function (socket) {
    socket.sendUTF("hello");
    console.log("connect");
};

io.on("connection", handleClient);

console.log("listen: "+ipaddress+" "+port);
server.listen(port, ipaddress);

Log file is this:

DEBUG: Sending SIGTERM to child...
DEBUG: Running node-supervisor with
DEBUG:   program 'server.js'
DEBUG:   --watch '/var/lib/openshift/5551a336e0b8cd4ea50000db/app-root/data/.nodewatch'
DEBUG:   --ignore 'undefined'
DEBUG:   --extensions 'node|js|coffee'
DEBUG:   --exec 'node'
DEBUG: Starting child process with 'node server.js'
DEBUG: Watching directory '/var/lib/openshift/5551a336e0b8cd4ea50000db/app-root/data/.nodewatch' for changes.
listen: 127.13.35.129 8080

Upvotes: 4

Views: 2218

Answers (1)

user2879327
user2879327

Reputation:

You are binding to the wrong port, you need to bind to 8080 like this:

var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;

Then you will still access your application with this url:

ws://js-camillorh.rhcloud.com:8000

or for secure websockets

wss://js-camillorh.rhcloud.com:8443

Upvotes: 1

Related Questions