2

I locally wrote a nodeJS app using socket.io and express modules. I wanted to use openshift for hosting. So I changed the main .js to server.js which seems to be the index equivalent of the openshift file and changed the server port setting to:

var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 3000);

as indicated in some posts. However after git commit, I am still getting:

remote: info: socket.io started remote: warn: error raised: Error: listen EACCES remote: DEBUG: Program node server.js exited with code 0 remote: remote: DEBUG: Starting child process with 'node server.js'

and the website doesn't work.

As the app is serving a html file, there are two more places, where the port is mentioned, which sit in the index.html that is served:

header:

<script src='//localhost:3000/socket.io/socket.io.js'></script>

and within javascript for the html file:

var socket = io.connect('//localhost:'+process.env.OPENSHIFT_NODEJS_PORT || 3000);

// intial vars and multi list from server socket.on('clientConfig', onClientConfig);

All files and modules are seemingly uploaded, but the EACCES error still prevails.

I get the feeling that maybe the header link to localhost:3000 might be the skipping point, but I am not sure. Anyone have any idea, what the problem is? Also, there is no : socket.io/socket.io.js file in the socket.io modules folder, which I find confusing.

Marcus Kirsch
  • 87
  • 2
  • 7

3 Answers3

8

I had recently developed a chat client application using socket.io and also had webrtc in it. I was able to deploy the app on openshift by making the following changes into code.

Client Side

Keep the include script tag in a relative manner like so

<script src="/socket.io/socket.io.js"></script>

While declaring io.connection, change the ip part to point the application to server in this format.

var socket = io.connect('http://yourapp-domain.rhcloud.com:8000/', {'forceNew':true });

8000 is for http and 8443 is for https

Server Side

The io and the server should both be listening on the same port and the order in which the statements are run should also be given attention.

Step 1: Declare the http server using app. ( app is obtained from express)

var express = require('express');var app = express();) var server = require('http').Server(app);

Step 2:

Declare io from socket.io and combine it with the server object. var io = require('socket.io').listen(server);

Step 3:

Now, allow the server to listen to openshift port and ip.

server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);

Please pay special attention to the order of the statements you write, it is the order which causes issues.

Udit Khandelwal
  • 326
  • 3
  • 15
  • This was exactly my problem. I was spending 5+ hours trying to find what I was doing wrong reading tons of articles that all lead in a similar direction but just didn't spell it out as clear as you did. I didn't realized that the server wasn't actually listening because I believe socket.io was already doing that! – Andrew Jul 10 '15 at 16:24
5

The server side of your websocket needs to listen on port 8080 on your openshift ip address, the CLIENT side needs to connect to your ws://app-domain.rhcloud.com:8000

  • 1
    So in the server.js it needs to be 8080 and in the html all is ws://app-domain.rhcloud.com:8000 for socket.io, yes? – Marcus Kirsch Mar 27 '14 at 18:36
  • Correct. You can also use wss://app-domain.rhcloud.com:8443 to use ssl –  Mar 27 '14 at 18:40
  • Okay I changed server.js to: – Marcus Kirsch Mar 27 '14 at 18:53
  • var server = require('http').createServer(app).listen(process.env.OPENSHIFT_NODEJS_PORT || 8080); – Marcus Kirsch Mar 27 '14 at 18:54
  • and the socket init : var socket = io.connect('wss://area56-multisockettest.rhcloud.com:8443'); – Marcus Kirsch Mar 27 '14 at 18:57
  • No good people: remote: DEBUG: Starting child process with 'node server.js' remote: DEBUG: Watching directory '/var/lib/openshift/533450f25973ca577c00008c/app-root/runtime/repo' for changes. remote: info: socket.io started remote: warn: error raised: Error: listen EACCES remote: DEBUG: Program node server.js exited with code 0 remote: remote: DEBUG: Starting child process with 'node server.js' – Marcus Kirsch Mar 27 '14 at 19:04
  • What type of cartridge are you using? Do you already have a web server running on your gear using port 8080? –  Mar 27 '14 at 19:40
  • Openshift standard nodejs-0.6 + modules thats all – Marcus Kirsch Mar 27 '14 at 19:50
  • You need to also bind to your OPENSHIFT_NODEJS_IP, along with the port, check out this example: https://github.com/openshift-quickstart/openshift-nodejs-http-and-websocket-example/blob/master/server.js –  Mar 27 '14 at 20:10
  • Replaced this: //var server = require('http').createServer(app).listen(port); var server = require('http').createServer(app); server.listen( port, ipaddress, function() { console.log((new Date()) + ' Server is listening on port 8080'); }); And the code is running now it seems, however it doesn't execute everything. What's the easiest way seeing the live console.log output? – Marcus Kirsch Mar 28 '14 at 13:22
2

I have a few notes on how to use WebSockets here: https://www.openshift.com/blogs/10-reasons-openshift-is-the-best-place-to-host-your-nodejs-app#websockets

You don't need any additional server-side changes after adapting your code to take advantage of environment variables (when available)

OpenShift's routing layer exposes your application on several externally-accessible ports: 80, 443, 8000, 8443.

Ports 8000 and 8443 are both capable of handling websocket connection upgrades. We're hoping to add support for WebSocket connections over ports 80 and 443 soon.

ʀɣαɳĵ
  • 1,992
  • 1
  • 15
  • 20
  • Okay that is rather confused me, I am still trying to test on teh first answer. – Marcus Kirsch Mar 27 '14 at 19:01
  • If you look above I am not using a particular IP on the server side to start the server. Is that a problem? – Marcus Kirsch Mar 27 '14 at 19:36
  • Yes. Also, "process.env" will only be in scope for your server-side code. If you want to access that information from client-side javascript, you'll need to establish another way to reference it. – ʀɣαɳĵ Mar 27 '14 at 21:32
  • I thought as much, having the main code running, but I think the relative .js references to other scripts is broken in the index.html. This works fine on my local server, why broken in openshift? Sooo many differences.... – Marcus Kirsch Mar 28 '14 at 13:54
  • I added 8080 for the server.js and the process.env. On the client side (index.html and socket.js, I added the ws:// xxx:8000 domain location. – Marcus Kirsch Jul 31 '14 at 08:28
  • DEBUG: Program node server.js exited with code 0 DEBUG: Starting child process with 'node server.js' Tue, 29 Jul 2014 13:51:04 GMT express deprecated app.configure: Check app.get('env') in an if statement at server.js:11:5 info: socket.io started warn: error raised: Error: listen EACCES DEBUG: Program node server.js exited with code 0 DEBUG: Starting child process with 'node server.js' Tue, 29 Jul 2014 13:51:06 GMT express deprecated app.configure: Check app.get('env') in an if statement at server.js:11:5 info: socket.io started warn: error raised: Error: listen EACCES – Marcus Kirsch Jul 31 '14 at 08:31
  • ==> app-root/logs/nodejs.log <== DEBUG: program 'server.js' DEBUG: --watch '/var/lib/openshift/53d6ce2f5973caa52c000023/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/53d6ce2f5973caa52c000023/app-root/data/.nodewatch' for changes. >PORT>8080>IP>127.3.72.1 info: socket.io started DEBUG: Sending SIGTERM to child... – Marcus Kirsch Jul 31 '14 at 08:34