42

I am starting to use NodeJS and Socket.IO. I am trying to set up a basic example with a NodeJS http server and establish a Socket.IO connection to the server.

I am also using angular JS and basically what I want is that when a user presses a button then the connection to the server is established. However, when I try it I get this error

GET http://localhost/socket.io/?EIO=2&transport=polling&t=1404288173776-3 net::ERR_CONNECTION_REFUSED

Here is my code:

server.js

var http = require('http');
var server= http.createServer(handler);
var io = require('socket.io')(server);

server.listen(8080);

function handler(req, res) {
    res.writeHead(200);
    res.end('Hello Http');
}

io.on('connection', function (socket){
    socket.emit('news', { hello: 'world' });
    console.log('connected!');
});

app.js

var app = angular.module('testApp', ['ngRoute']);

app.controller('TestCtrl', function ($scope){

    $scope.msg= "";

    $scope.try = function (){

        $scope.msg= "ALO"

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

        socket.on('news', function (data) {
            console.log(data);
        });

    };

});

And on my test.html file:

<body ng-controller="TestCtrl">

    <h2>{{msg}}</h2>
    <button ng-click="try()">Try</button>

    <script src="../js/angular.min.js"></script>
    <script src="../js/angular-route.min.js"></script>
    <script src="../js/app.js"></script>

    <script src="../node_modules/socket.io/node_modules/socket.io-client/socket.io.js"></script>

<!-- <script src="/socket.io/socket.io.js"></script> -->

</body>

I think that the error might be something related to the path I use when including the socket.io.js I have tried also using directly localhost:8080 in the path because I read it could be a solution but that didn't work. So please, I appreciate any help given. Thanks!

1
  • 1
    I had also the same error. After this post I just cross checked the URL which I had given incorrect.Thanks. Sep 11, 2019 at 18:05

6 Answers 6

66

I faced exactly the same issue. I did not see the ERR_CONNECTION_REFUSED in socket.io@~0.9. The error surfaced after I upgraded socket.io to 1.3.

I solved it by simply removing the URL from the client side constructor:

Change

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

To

var socket = io();  

As the socket.io tutorial showed: http://socket.io/get-started/chat/

3
  • 2
    Using the implicit URL fixed it for me as well, thanks! Sep 27, 2015 at 15:33
  • Nice answer. Just complementing what the example says: "Notice that I’m not specifying any URL when I call io(), since it defaults to trying to connect to the host that serves the page." Jul 5, 2016 at 23:38
  • 7
    What about using different port or not hosted on local? Oct 25, 2018 at 14:54
16

If your server is listening on port 8080, you need to connect to that port.

var socket = io('http://localhost:8080');

And as you're using a relative path, the socket.io lib will be served fine.

<script src="/socket.io/socket.io.js"></script>
1
  • 1
    I tried these two solutions before (sorry I didn't mention that) but I get a file not found error for the socket.io.js path, that's why I tried the other one. I don't know what could be the problem. I installed socket.io using npm install socket.io and the node_modules folder containing socket io are in my users folder on windows for some reason and not in the node_modules folder that is in program files. Could that have something to do with the problem? I tried to install globally to see if it changed the location folder but it didn't :/ I don't know what else to try
    – tampeta
    Jul 2, 2014 at 8:47
3

It turns out that the socket.io was being installed in a funny folder and therefore it was not being recognised or loaded. I solved this by creating a package.json and declaring the dependencies there so that when I do npm install it installs the folder in the right place.

Also Ben Fortune was right and I also modified that...I hope this helps other people because I struggled A LOT to figure that out :)

0
1

I was accessing IP of my cloud server to chat. suddenly The server was down.Then I got this polling error. Finally I figured out The problem is with that server.make sure your server is up.

I changed IP address to zero. please assume your server address

socket = io.connect('0.0.0.0:8110');
0
const { Server } = require("socket.io");
const app = express();
let port = process.env.PORT || 8080;
const server = http.createServer(app);
server.listen(port, () => console.log("App listening at localhost:" + port));

It work for me :)

See doc

0
// SERVER running on http://localhost:9000

    const PORT = 9000;    
    const http = require('http');
    
    const httpServer = http.createServer()
    
    
    const io = require("socket.io")(httpServer, {
        cors: {
            origin: "http://localhost:3000", // client address 
        },
    });   
    
    
    io.on("connection", (socket) => {
        console.log(socket)
    })
    
    httpServer.listen(PORT);



// CLIENT


    import { io } from 'socket.io-client'
    
    const URL = "http://localhost:9000";
    const socket = io(URL, { autoConnect: false });
    socket.connect();
    console.log(socket)
    
    socket.on("connect_error", (err) => {
        console.log(`connect_error due to ${err.message}`);
    });

The socket should be listening to the app (httpServer) and app should listen to the port. Don't mess it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.