Last Updated: December 14, 2016
·
14.69K
· bsorin

Socket.io and OpenShift Websockets

Socket.io works great with OpenShift's websockets. All you need to do is to configure the socket.io to use the "websocket" transport.
The rest of transports won't work.

self.app = express();
self.server = require('http').createServer(self.app);
self.io = io.listen(self.server);
self.io.configure(function(){
    self.io.set("transports", ["websocket"]);
});

9 Responses
Add your response

https://www.openshift.com/blogs/paas-websockets
It seems, we have to use the port "8000" and "8443" for websockets.
I've been trying to change this on the client using

//server
io = require('socket.io').listen(8000);
//client
var socket = io.connect("http://something:8000");

But request keeps going to port 80 ... Is there any work around for this ?

over 1 year ago ·

On the server side you should use the default port provided by openshift:

self.port      = process.env.OPENSHIFT_INTERNAL_PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;

In this way I initialized the socket.io on the server side:

// socket.io initialization on the server side
self.initializeSocketIO = function() {
        self.server = require('http').createServer(self.app);
        self.io = require('socket.io').listen(self.server);
        self.io.enable('browser client minification');  // send minified client
        self.io.enable('browser client etag');          // apply etag caching logic based on version number
        self.io.enable('browser client gzip');          // gzip the file
        self.io.set('log level', 1);                    // reduce logging

        self.io.set('transports', [
                'websocket'
            ]);
        return this;
    }

    self.addSocketIOEvents = function() {
        self.io.sockets.on('connection', function (socket) {
          socket.emit('news', { hello: 'world' });
          socket.on('my other event', function (data) {
            console.log(data);
      });
    });
}

/**
 *  Initializes the sample application.
 */
self.initialize = function() {
    self.setupVariables();
    self.populateCache();
    self.setupTerminationHandlers();

    // Create the express server and routes.
    self.initializeServer();
    self.initializeSocketIO().addSocketIOEvents();
};

On the client side I used it in this way (index.html):

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body>
ok
</body>

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('ws://you-app.rhcloud.com:8000/');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
</html>

It should work fine for you. You used a bad port number.

over 1 year ago ·

thanks! helped me!

over 1 year ago ·

Hi
Can you explain TO me if I understood well, we have to use different ports on server (8080) and client (8000)???
Thank you
I am a little confused.

over 1 year ago ·

@camillo777 yes. So, for plain WebSockets ws:// you will use port 8000 and for secured connections wss:// port 8443. Here’s an example:

http://app-lovingwebsockets.rhcloud.com/ <= your current HTTP URL
http://app-lovingwebsockets.rhcloud.com:8000/ <= WebSockets enables HTTP URL

https://app-lovingwebsockets.rhcloud.com/ <= your current HTTPs URL
https://app-lovingwebsockets.rhcloud.com:8443/ <= WebSockets enables HTTPs URL

You can find more details here: https://www.openshift.com/blogs/paas-websockets

over 1 year ago ·

Did you mean:
ws://app-lovingwebsockets.rhcloud.com:8000/

Instead of
http://app-lovingwebsockets.rhcloud.com:8000/

?

over 1 year ago ·

@camillo777 yes, you should use ws://app-lovingwebsockets.rhcloud.com:8000/

over 1 year ago ·

Hi, did you try it on openshift?
Is your url working? Or is it only an example?

On my site I have a connect/disconnect behavior:
ws://js-camillorh.rhcloud.com:8000

Thank you

over 1 year ago ·

On Server use Port 8080 and start socket.io as usual with http or express.
On Client do
NOT: var socket = io.connect(...
DO: var socket = io();

Thats it. Found at heroku and works at OpenShift as well.
https://devcenter.heroku.com/articles/node-websockets#create-a-socket-io-client

over 1 year ago ·