0

I have installed node.js on my shared hosting with GoDaddy and can run node apps.

I don't exactly know where the problem is, but here's what's going on. I run the server, and it starts listening on a port (I tried a range from 49000 to 56000).

But when the client tries to connect, i.e. access '/' on the server, the node is silent, therefore it doesn't receive any connection requests. So that kind of narrows it down to socket.io.

In the console within a few seconds it spits out this: socket.io-1.4.5.js:1 GET http://website.com:55872/socket.io/?EIO=3&transport=polling&t=LQRdEP9 net::ERR_CONNECTION_TIMED_OUT

I have tried to source socket.io from the cdn as well as my own directory - nothing.

This is what the server looks like:

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

//------------- GLOBAL VARIABLES ---------------

    SERVER_PORT = 55872;

    connections = [];

//----------------------------------------------

server.listen(SERVER_PORT, "127.0.0.1");

console.log('................................................');
console.log('=> Server is listening on port: ' + SERVER_PORT);
console.log('................................................');

app.get('/', function(req, res){

    console.log('=> Loading index.html!');

    res.sendFile(__dirname + '/index.html');

});

// ----------- MIDDLEWARE ----------------------

// ----------- CONNECTIONS ---------------------

io.sockets.on('connection', function(socket){

    connections.push(socket);
    console.log('=> Client connected! Total connected: %s', connections.length);

    // Disconnect socket
    socket.on('disconnect', function(data){

        connections.splice(connections.indexOf(socket), 1);
        console.log('=> Client disconnected! Total connected: %s', connections.length);    

    });

Client:

 $(function(){

        //--------- SOCKET INIT --------------
        var socket = io.connect('http://website.com:55872/');
});

Is the issue with socket.io or the node, or me...?

Micard
  • 379
  • 2
  • 15
  • Does the express `index.html` serving work? – qxz Aug 18 '16 at 03:21
  • It is very common that shared hosting does not necessarily support long running server processes like node.js or webSocket/socket.io connections because of the added resource cost or has to be configured specially for it. Often times you need a VPS. You should check with GoDaddy on this. See http://stackoverflow.com/a/27283415/816620 – jfriend00 Aug 18 '16 at 03:32

2 Answers2

0

GoDaddy does not currently support running node.js servers on shared hosting. You would need a VPS account. GoDaddy did buy Nodejitsu so presumably they will be expanding their node.js offerings, but as of this moment you can't run node.js servers on GoDaddy shared hosting.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
-1

I had similar problems getting socket.io to work with express. Here is a snippet from my working server code:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
  // to allow cross-origin requests
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");

  //...
});

var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(process.env.PORT, process.env.IP, function() {
  console.log("Listening!");
});

io.on('connection', function (socket) {
  console.log("Got connection!");
  //...
});

It's slightly different from what you have, and may do the trick. Otherwise, your problem probably isn't with the node.js code.

This was hosted on a free Cloud9 workspace. I've also had node servers on Heroku... you should try one of those platforms, as they have official support for node.

qxz
  • 3,814
  • 1
  • 14
  • 29
  • I think this is a question about hosting support for node.js/socket.io, not about specific code. – jfriend00 Aug 18 '16 at 03:39
  • It seems like the OP doesn't know what the problem is. He even suggests that it might be "socket.io or the node, or me". This is a valid answer to try to solve his problem – qxz Aug 18 '16 at 03:40
  • Except that GoDaddy doesn't support node.js servers on shared hosting at all. You can't fix it with this code. Now, if you can show this code works on GoDaddy shared hosting, then it could be an answer. – jfriend00 Aug 18 '16 at 03:41
  • Ok, didn't know that. You should write an answer to address that – qxz Aug 18 '16 at 03:43
  • @Micard - You can run node apps. You can run node.js servers on a VPS account. You can't run node.js servers on GoDaddy shared hosting. If you have a reference to the contrary (something new from GoDaddy), please post it. – jfriend00 Aug 18 '16 at 03:57