Subtiv Subtiv - 6 months ago 148
Javascript Question

Websocket failure in openshift / nodeJS

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.

[EDIT] : As suggested by Xodrow

this.socket = io('...', {forceNew: true, transports: ['websocket']});


doesn't really change anything

Answer

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.

Comments