3
votes

I know quit some similar questions have been asked here, however none of the proposed solutions seem te work. (I'll post relevant answers below)

I can't get a simple websocket app to work on openshift. The app itself works locally. [Edit: i'm using socket.io]

The relevant code:

Server:

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

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

io.on('connection', function (socket) {
… 
});

index.html

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

Client.js

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

result:

  • GET http://****-****.rhcloud.com:8000/socket.io/?EIO=2&transport=polling&t=1446734825100-0 net::ERR_CONNECTION_TIMED_OUT *

Stuff I tried

use port 80 instead of 8000 (on client)

result: WebSocket connection to 'ws://app-domain.rhcloud.com/socket.io/?EIO=2&transport=websocket&sid=rsqAz33O_aYSFfN3AAAD' failed: Error during WebSocket handshake: Unexpected response code: 400

Interchange server.list(… and var io = require('…

no result

Use ip address of server instead of http://app-domain.rhcloud.com

no result

Similar questions

The following questions kind of deal with the same issue, however none of the proposed solutions worked so far

Socket.io and node on OpenShift

OpenShift NodeJS deployment : socket.io index.html port assignment, etc

unable to deploy nodejs socket io application on openshift

Can't get socket.io and nodejs running with OpenShift

Socket.io.js returns 404 on OpenShift

Also I cloned some github examples of socket.io/openshift but all of them kind of deal with the same issue.

Either I'm doing something completely wrong or something funny is going on over at openshift.

Thanks in advance.

2
Have you tried to force the websocket transport in the client ? this.socket = io('...', {forceNew: true, transports: ['websocket']});Xodrow
Have you tried checking the OpenShift logs by running rhc tail <yourappname> when trying to connect? It may provide some more clues where the problem is.Jiri Fiala
@Jiri : yeah, of course… however there is no syntactic error, so there's not that much logging going on. So no aid there!Subtiv
@Xodrow I tried it… no significant changeSubtiv
@Subtiv any luck on this? I have also been trying this.Sayanee

2 Answers

1
votes

I've finally cracked it. Apparently by hardcoding the transport-configuration on the server side it works.

var io = require('socket.io', {
        transports: ['websocket']
    })(http);

Hope this helps someone in the future.

0
votes

I encountered the same problem when I made a simple chat app using socket.io on openshift. I viewed a lot comments on Stack Overflow. Finally, I changed two places.

Firstly, add this:

 app.use(function(req,res,next)
        res.header("Access-Control-Allow-Origin", "*");
        res.header("Access-Control-Allow-Headers", "X-Requested-With");
        res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        next();})

Secondly, add an option when requiring the socket.io module.

  var io = require('socket.io', {
      transports: ['websocket']
  })(http);