abaracedo
abaracedo

Reputation: 1454

WebSocket connection to OpenShift app failed

I created an app with NodeJS and I'm using ws module. If I test the app in localhost it works and there isn't any problem to connect websockets. Now I've upload the app to Openshift and when I try to access from the client it returns that is not possible to stablish a connection to the websocket.

If I do a tail in putty to my app I have this message: DEBUG: This type of response MUST NOT have a body. Ignoring data passed to end().

The code that I have in the server is:

#!/bin/env node

//Openshift variables
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "192.168.69.42";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;


//NodeJS require modules
var Enum = require('enum');
var WebSocketServer = require('ws').Server
    wss = new WebSocketServer({host:ipaddress, port:port});
var fs = require('fs');


wss.on('connection', function(ws) {
    console.log((new Date()) + ' Connection from origin: ' + ws._socket.remoteAddress);
});

console.log((new Date()) + " Server is listening on: " + ipaddress + ':' port);

And in the client:

var ws = new WebSocket("ws://192.168.69.42:8080/");

ws.onopen = function() {
    console.log("Connected.");
    ws.send("This is the client speaking.");
};

Upvotes: 10

Views: 10668

Answers (2)

andbas
andbas

Reputation: 591

For all WebSocket connections on OpenShift you need to use port 8000 (for Secured sessions it would be 8443). So, your server example works well (I run them after removing the unnecessary line var Enum = require('enum');, you just need to hardcode the port on client to 8000:

var ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000"); 

ws.onopen = function(){
  console.log('opened')
}

More information here.

Upvotes: 19

user2879327
user2879327

Reputation:

Here is an example on github that works that you can check out: https://github.com/developercorey/openshift-nodejs-http-and-websocket-example

Upvotes: 3

Related Questions