Joined January 2013
·

Sorin Baba

Web Developer
·
Romania
·
·

Posted to Socket.io and OpenShift Websockets over 1 year ago

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

Posted to Socket.io and OpenShift Websockets 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

Posted to Socket.io and OpenShift Websockets 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.

Achievements
227 Karma
15,112 Total ProTip Views