2

This is the error message I am getting in the web console on my live node.js heroku app: polling-xhr.js:264 GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M2MDZUw net::ERR_CONNECTION_REFUSED

When I run my program locally, it all works fine without any problem. I have listed the server to listen to whatever is in the environment PORT or port 3000 if there's nothing there. Below is my relevant server.js code, and package.json code, as well as the client code used to connect to the server.

server.js

var express = require("express");
var socket = require("socket.io");

var app = express();
var server = app.listen(process.env.PORT || 3000)

package.json

{
  "name": "testProject",
  "version": "1.0.0",
  "description": "test project using node.js + sockets.io",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "sockets",
    "p5",
    "node"
  ],
  "author": "henry zhu",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "socket.io": "^2.0.4"
  }
}

Client-side code to connect to server:

var socket = io.connect("http://localhost:3000");

The version of socket.io I am using client side is 2.0.4.

Henry Zhu
  • 2,488
  • 9
  • 43
  • 87
  • https://stackoverflow.com/questions/27393705/socketio-get-http-localhost3000-socket-io-eio-3transport-pollingt-1418187 , https://github.com/socketio/socket.io-client/issues/1097 , https://github.com/socketio/socket.io-client/issues/1053 – wrangler Dec 27 '17 at 07:39
  • @MikaS What do you mean by connection code for the browser? – Henry Zhu Dec 27 '17 at 21:23
  • I think he is referring to the code at the frontend (client side) which is connects to the server. Please include which version of `socket.io` you use on the client side. – SarathMS Dec 27 '17 at 21:59
  • @SarathMS I have updated the frontend code which connects to the server and the version of socket.io I use on the client side. – Henry Zhu Dec 27 '17 at 22:14

1 Answers1

3

When you connect like this

var socket = io.connect("http://localhost:3000");

on the client-side you are actually trying to connect to your own computer on port 3000. If you fire up the server on your computer they might even start communicating. However, you want to connect to the server you are hosting on Heroku. If the client is served on the same domain as the server you can simply do

var socket = io();

The socket will then connect to it's own domain on the port it's served on (usually 80). It works both locally and on Heroku. If you want to change port you can use:

var socket = io(':3000');

If the client is served by another server than the socket server you will have to supply the whole URL:

var socket = io('https://example.herokuapp.com');

Remember that when you use var server = app.listen(process.env.PORT || 3000) you are not running on port 3000 on Heroku. Heroku will set the PORT environment variable for you and to the outside world the port is 80.

Mika Sundland
  • 18,120
  • 16
  • 38
  • 50