I have read that OpenShift listening on port 8080 server side. I have also read that they force web sockets to connect to port 8000 client side.
Nowhere have I read that I need to mention port 8000 server side, since apparently OpenShifts apache reverseProxy is supposed to route 8000 to 8080 (and it is up to the application to decide what type of connection it is?)
I have read a whole lot, tried everything, and I really just can't figure this shite out.
Site runs fine in local host when listening on 8080 and connecting with var socket = io();
client side.
On OpenShift, refer to the following:
Client: 8000, Server: 8080
Runs and establishes WS connection, but no response from server. No .on('connection', ... )
to be had. Makes sense, not listening for 8000.
Client: 8000, Server: 8080, Server2: 8000
Attempting to have 2 separate servers and the site won't build (timeout when waiting for port 8080 available)
Client: 8000, Server: 8000
Site won't build (same as above)
Client: just connect using io()
server: 8080
site runs as expected but falls back to xhr long polling
Does anyone know of a straightforward tutorial or example of someone using up to date Socket.io, OpenShift, and Express 4? The worst part is that every example you see people are starting their servers in different (deprecated, newer than I know about?) ways. its madness for something that seems so simple (and even advertised).
FYI: The last port combo, connecting with simply io();
and listening on port 8080 server-side was working flawlessly early. Are websockets temperamental on OpenShift?
As martin suggested: use OpenShift's environmental variables found at https://developers.openshift.com/managing-your-applications/environment-variables.html. You can also get the port this way.
However, unlike the variable Martin named, does the code I got by default from a 'latest version'-cartridge contain env.NODE_IP and env.NODE_PORT. This may be different if you're using the 0.10 node cartridge.
after battling with this for a bit thought i would share my setup using openshift 3, nodejs 6+, express 4 and socket.io 1.
full info is below but just to summarize some of the main points that differ from what i saw suggested by other sources:
- server use port 8080 and ip of 0.0.0.0
- its not necessary to set transports option to "websocket" on client or server
- you do not need to specify a port number on client
- for remote client connections make sure to adjust not just ws/wss for socket url but also the path which is different on openshift when you create a secure route for your nodejsapp
for server code i am using the following. note some older articles floating around seem to indicate 127.0.0.1 as the port, but it appears to in fact be 0.0.0.0 on openshift 3
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io').listen(http);
var port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
var ip_address = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
http.listen(port, ip_address, function(){
console.log( "Listening on " + ip_address + ", port " + port );
});
for client code, if the page you are trying to connect from is served off the openshift node app than the connection is quite easy..
// works for http or https
var socket = io.connect();
if you are trying to connect from a remote client then you just need to provide path to either secure or unsecure route to your app and use ws/wss. there is no need to specify a port of 8000 or 8443!
// http
var socket = io.connect('ws://ROUTENAME-APPNAME.regionandstuff.openshiftapps.com');
// https
var socket = io.connect('wss://SECUREROUTENAME-APPNAME.regionandstuff.openshiftapps.com');