TheRuthlessHacker
TheRuthlessHacker

Reputation: 105

ERR_CONNECTION_REFUSED from socket.io

I'm trying to run Node.js application. With SSH I am able to run 'node server.js':

var express = require('express');
var app = express();
var server = app.listen(5431);
app.use(express.static('public'));
console.log("### RUNNING ###");
var socket = require('socket.io');
var io = socket(server);

and it indeed logs and throws no errors. But when I open client-browser app, I get following output in console:

Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost:5431/socket.io/?EIO=3&transport=polling&t=MWCsv9T net::ERR_CONNECTION_REFUSED

Client connects with:

var socket = io.connect('http://localhost:5431');

I've tried connecting both with IP and domain, with same result. App worked fine on local machine.

I've checked open ports with following php script:

    for($i=0; $i<10000; $i++) {
    portCheck($i);
  }

  function portCheck($port) {
    if(stest('127.0.0.1', $port))
      echo $port . "<br>";
  }

  function stest($ip, $portt) {
    $fp = @fsockopen($ip, $portt, $errno, $errstr, 0.1);
    if (!$fp) {
        return false;
    } else {
        fclose($fp);
        return true;
    }
  }

and I did get:

enter image description here

As it listed 5431 I'm assuming that port is indeed open.

I have no idea then what can cause this error.

Upvotes: 2

Views: 14057

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123260

var socket = io.connect('http://localhost:5431');

This means the client will connect to the local machine - where local is from the perspective of the system where script is executing. Since the script is executing in the browser it means that your application is expected to run on the clients machine where the browser is running.

Failed to load resource: net::ERR_CONNECTION_REFUSED
GET http://localhost:5431/socket.io/?EIO=3&transport=polling&t=MWCsv9T net::ERR_CONNECTION_REFUSED

While you don't explicitly say this a later statement (see below) suggests that this is done on a different machine than your application is running on. If your application is running on host A and your browser on host B then localhost inside the client browser (where script gets executed) refers to host B and not host A.

App worked fine on local machine

This is expected since in this case localhost is the machine where your application is actually running on (same machine for application and browser).

Upvotes: 3

Related Questions