GET 404 (File not found) and net::ERR_CONNECTION_REFUSED from socket.io
<p>I know similar issues has been posted on here before. But I have read and tried all the proposed solutions and I am still not getting it to work.</p>

<p>I am trying to connect two devices using socket.io. But every time I get this error:</p>

<blockquote>
  <p>GET <a href="http://localhost:4444/socket.io/?EIO=3&amp;transport=polling&amp;t=LZkz-Kk" rel="nofollow noreferrer">http://localhost:4444/socket.io/?EIO=3&amp;transport=polling&amp;t=LZkz-Kk</a> 404 (File not found)     polling-xhr.js:261</p>
  
  <p>Request.create @   polling-xhr.js:261<br>
  Request @   polling-xhr.js:166<br>
  XHR.request @   polling-xhr.js:93<br>
  XHR.doPoll  @   polling-xhr.js:123<br>
  Polling.poll    @   polling.js:118<br>
  Polling.doOpen  @   polling.js:63<br>
  Transport.open  @   transport.js:80<br>
  Socket.open @   socket.js:240<br>
  Socket  @   socket.js:119<br>
  Socket  @   socket.js:29<br>
  Manager.open.Manager.connect    @   manager.js:213<br>
  Manager @   manager.js:68<br>
  Manager @   manager.js:37<br>
  lookup  @   index.js:60<br>
  socketSetup @   sketch.js:9<br>
  init    @   sketch.js:69<br></p>
</blockquote>

<p>That error is then always followed by this, but I guess it is an issue that follows because it can't find my file, right?</p>

<blockquote>
  <p>GET <a href="http://localhost:4444/socket.io/?EIO=3&amp;transport=polling&amp;t=LZk-142" rel="nofollow noreferrer">http://localhost:4444/socket.io/?EIO=3&amp;transport=polling&amp;t=LZk-142</a> net::ERR_CONNECTION_REFUSED polling-xhr.js:261</p>
</blockquote>

<p>I have tried every different way to SRC the socket.io file that I have ran into online, these are some examples:</p>

<pre><code>&lt; script type="text/javascript" src="http://localhost:4444/socket.io/socket.io.js"&gt;&lt; /script &gt;&lt;br&gt;
&lt; script type="text/javascript" src="http://myipadress:4444/socket.io/socket.io.js"&gt;&lt; /script &gt;&lt;br&gt;
&lt; script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.7.2/socket.io.js"&gt;&lt; /script &gt;&lt;br&gt;
&lt; script type="text/javascript" src="/socket.io/socket.io.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; script src="libraries/socket.io.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; script type="text/javascript" src="/node_modules/socket.io-client/dist/socket.io.js"&gt;&lt; /script&gt;
</code></pre>

<p>Basically, they all give me the same error, except if I leave the SRC out, as some say the socket link shouldn't need to be implied. In that case I get:</p>

<blockquote>
  <p>Uncaught ReferenceError: io is not defined(…) sketch.js:9
  socketSetup    @   sketch.js:9<br>
  init    @   sketch.js:69</p>
</blockquote>

<p>This is my server side, <strong>app.js:</strong></p>

<pre><code>var express = require('express');&lt;br&gt;
var app = express();&lt;br&gt;
var PORT = 4444;&lt;br&gt;
// Routing&lt;br&gt;
app.use('/', express.static(__dirname + '/public'));&lt;br&gt;
// Socket.io setup&lt;br&gt;
var server = require('http').createServer(app);&lt;br&gt;
var io = require('socket.io')(server);&lt;br&gt;
server.listen(PORT, function(){&lt;br&gt;
    console.log('Server listening at port ' + PORT);&lt;br&gt;
});&lt;br&gt;
&lt;br&gt;
io.on('connection', function(socket) {&lt;br&gt;
A bunch of my io code.&lt;br&gt;
)};
</code></pre>

<p>This is my index.html code:</p>

<pre><code>&lt; !DOCTYPE html&gt;&lt;br&gt;
&lt; html&gt;&lt;br&gt;
&lt; head&gt;&lt;br&gt;
  &lt; meta charset="UTF-8"&gt;&lt;br&gt;
    &lt; link rel="stylesheet" href="css/style.css"&gt;&lt;br&gt;
&lt; /head&gt;&lt;br&gt;
&lt;br&gt;
&lt; body&gt;&lt;br&gt;
&lt;br&gt;
&lt; script type="text/javascript" src="http://localhost:4444/socket.io/socket.io.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; !-- &lt; script type="text/javascript" src="/socket.io/socket.io.js"&gt;&lt; /script&gt;-- &gt;&lt;br&gt;
&lt; script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/p5.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.5/addons/p5.dom.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; script type="text/javascript" src="libraries/jquery-min.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; script type="text/javascript" src="sketch.js"&gt;&lt; /script&gt;&lt;br&gt;
&lt; /body&gt;&lt;br&gt;
&lt; /html&gt;&lt;br&gt;
</code></pre>

<p><strong>package.json</strong></p>

<pre><code>{
  "name": "public",
  "version": "1.0.0",
  "description": "Test",
  "main": "index.html",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
  },
  "author": "kim",
  "license": "ISC",
  "dependencies": {
    "express": "^4.14.0",
    "socket.io": "^1.7.2"
  }
}
</code></pre>

<p>This is the important part of my client side, <strong>sketch.js:</strong></p>

<pre><code>var app = app || {};

app.main = (function() {

var socket;

var socketSetup = function(callback){
    console.log('Called socketStart.');
      socket = io.connect();
      socket.on('movement', function(data) {
          Socket listeners..
        }
      );  
      callback();    
  } 
var init = function(){
    console.log('Initializing app.');
    //var myp5 = new p5(sketch, 'canvas-container');
    socketSetup(sketch);
  };

  return {
    init: init
  };

})();

window.addEventListener('DOMContentLoaded', app.main.init);
</code></pre>

<p>I have tried some different setups, but this logically should work?^^</p>

<p>And at last, here is my project structure:</p>

<pre><code>app.js
package.json
node_modules
public
sketch.js
index.html
css
libraries
</code></pre>

<p>I even tried to npm install socket.io locally in the public folder, but that didn't work either.</p>

<p>Can anybody see what I am doing wrong? Is there something I haven't tried that I should? What has worked for you?</p>

<p>Sorry for such a long post, I just wanted to clear what I have tried out to avoid unnecessary answers. I even tried uninstalling and removing all node and npm files and reinstalling it all.</p>