1

My little game server works fine when run locally, but when I put it up on my linode I get errors.

Full source: https://github.com/raimondi1337/null-terminus

Relevent Server code:

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

var clients = {};

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

Relevant Client Code:

<body>
<script src="/socket.io/socket.io.js"></script>
<h3 id="#player"></h3>
<canvas id="canvas" width="750" height="500"></canvas>
<script>var socket = io();

Run as is, I get:

GET http://staging.dustinraimondi.com/socket.io/socket.io.js 
(index):8 Uncaught ReferenceError: io is not defined

If I change the socket.io script tag to the latest socket.io CDN I get : socket.io-1.4.5.js:1 GET http://staging.dustinraimondi.com/socket.io/?EIO=3&transport=polling&t=LR3DCHp 404 (Not Found)

If I change io(); to io.connect(23.239.8.165:3000); as per Socket.IO only works locally io is still undefined and the 'GET' fails. Same if I use 'io.connect();', same if I specify my server's IP and I use the socket.io CDN.

What web concept am I not understanding here?

EDIT: Socket.io is installed on the server via NPM, the server is running.

Community
  • 1
  • 1
Strelok
  • 85
  • 11
  • No idea how Linode works, but how does it map requests from port 80 (on which your website seems to be accessible) to port 3000 (on which your Express app is running)? – robertklep Aug 25 '16 at 20:25
  • Linode is just a vps. I have CentOS7 installed and am running nginx as my webserver. Is the problem with my nginx configuration possibly? – Strelok Aug 25 '16 at 20:46
  • Could be, yes, although it's generally a pretty simple setup. [Here is a gist](https://gist.github.com/robertklep/f7221778f475832d6d236db7c180488e) from my own server, which also uses `socket.io` (and proxies websocket connections as well). – robertklep Aug 25 '16 at 20:54

2 Answers2

0

It looks like your server doesn't allow connections to port 3000. Verify that your firewall allows tcp traffic through this port. For calling the io script, you must call http://staging.dustinraimondi.com:3000/soket.io/?EIO=3&transport=polling&t=LR3DCHp

Nginx is running on port 80, so, when you request staging.dustinraimondi.com you're sending a request to port 80. If you're not using nginx, you can shut nginx down and set your nodejs app to listen on port 80 instead of port 3000.

Another option is to configure nginx (i'm not an nginx expert) to forward requests to a subdomain to port 3000. So, you could create a subdomain like io.dustinraimondi.com and when you request any of your nodejs project services nginx could forward the request to localport 3000.

adonike
  • 1,038
  • 10
  • 8
0

The problem was not with my code, but with my nginx server configuration. This block had to be added to my nginx configuration for this subdomain:

location /socket.io/ {
    proxy_pass http://localhost:3000;       
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header Host $host;
}

source: Socket.io with nginx

Community
  • 1
  • 1
Strelok
  • 85
  • 11