socket.io icon indicating copy to clipboard operation
socket.io copied to clipboard

net::ERR_CONNECTION_REFUSED

Open SametAylak opened this issue 10 years ago • 13 comments

Hey guys i'm trying to basic socket.io communication and i got a error as u see. Please share your solutions :) and i'm newbie so i copy paste codes please ignore them :)

This is my server.js

var socket = require( 'socket.io' ); var express = require('express'); var app = express(); var server = require('http').createServer(app); var io = socket.listen( server ); var port = 3000; server.listen(port, function () { console.log('Server listening at port %d', port); }); io.on('connection', function (socket) { io.sockets.emit('hey', 'hey i'm sent from server'); });

This is my index.html

var socket = io.connect('http://localhost:3000');
socket.on('hey', function(data){
   console.log(data);
});

And this is my file structure

  • public_html
    • node_modules
    • index.html
    • server.js

SametAylak avatar Feb 05 '16 07:02 SametAylak

Hi,

You have parse error, try to like this :

io.on('connection', function (socket) { io.sockets.emit('hey', 'hey i'm sent from server');

Also you can check this http://www.kodrella.com/nodejssocket-io-ile-basit-bir-chat-uygulamasi-yapalim/ ,

2016-02-05 9:28 GMT+02:00 Samet Aylak [email protected]:

Hey guys i'm trying to basic socketio communication and i got a error as u see Please share your solutions :) and i'm newbie so i copy paste codes please ignore them :)

This is my serverjs

var socket = require( 'socketio' ); var express = require('express'); var app = express(); var server = require('http')createServer(app); var io = socketlisten( server ); var port = 3000; serverlisten(port, function () { consolelog('Server listening at port %d', port); }); ioon('connection', function (socket) { iosocketsemit('hey', 'hey i'm sent from server'); });

This is my indexhtml

var socket = ioconnect('http://localhost:3000'); socketon('hey', function(data){ consolelog(data); });

And this is my file structure

  • public_html
    • node_modules
    • indexhtml
    • serverjs

— Reply to this email directly or view it on GitHub https://github.com/socketio/socket.io/issues/2431.

Teşekkürler mehmetalp.com.tr http://mehmetalp.com.tr

mehmetakifalp avatar Feb 05 '16 07:02 mehmetakifalp

i checked that site and i change my codes, it's running on localhost but when i upload host from ftp it's still getting same error :/

SametAylak avatar Feb 05 '16 07:02 SametAylak

What is your error? Have you nodejs on your server? Should be change localhost to yourhost.com, also the port should be available.

2016-02-05 9:53 GMT+02:00 Samet Aylak [email protected]:

i checked that site and i change my codes, it's running on localhost but when i upload host from ftp it's still getting same error :/

— Reply to this email directly or view it on GitHub https://github.com/socketio/socket.io/issues/2431#issuecomment-180241550 .

Teşekkürler mehmetalp.com.tr http://mehmetalp.com.tr

mehmetakifalp avatar Feb 05 '16 07:02 mehmetakifalp

That issue was closed automatically. Please check if your issue is fixed with the latest release, and reopen if needed (with a fiddle reproducing the issue if possible).

darrachequesne avatar Jan 12 '17 15:01 darrachequesne

i have the same error. i tried hosting it using my own laptop. it works when i access the site (on my laptop, or on other laptop/devices connected to my localnetwork) using my public ip. but when others (from outside my localnetwork) access the site, the client's connection refuses.. i am not sure what could be the problem. i was hoping that someone came up with a better solution. but to my failure, i found nothing. =(

louiepaguilar avatar Apr 07 '17 00:04 louiepaguilar

@vainell Can you show your socket.io-client config ? If you try to access with public IP, make sure you config socket.io-client with this public ip too. For example: You public IP is 192.168.2.77 and you server is listening at port 8080 Your socket.io-client config should be var socket = io('http://192.168.2.77:8080');

hienphamlabs avatar Apr 07 '17 06:04 hienphamlabs

@dhiefphams, thank you for your response, but i already solved my issue.. I just forgot to host the port which my server and client's are using. thats why im getting a response error.. But it now works.. again, thank you!

louiepaguilar avatar Apr 07 '17 08:04 louiepaguilar

hi @vainell i have the same problem. what did you mean by saying "I just forgot to host the port which my server and client's are using". I didn't get you saying host the port.

PiroozMB avatar Nov 01 '19 20:11 PiroozMB

I was hosting it using my own pc. i forgot to indicate the port

On Sat, 2 Nov 2019, 4:02 AM PiroozMB [email protected] wrote:

hi @vainell https://github.com/vainell i have the same problem. what did you mean by saying "I just forgot to host the port which my server and client's are using". I didn't get you saying host the port.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/socketio/socket.io/issues/2431?email_source=notifications&email_token=AGEWYYPFJ3XGRMV2BC2RELDQRSDMXA5CNFSM4B2QJCF2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC4AKXI#issuecomment-548930909, or unsubscribe https://github.com/notifications/unsubscribe-auth/AGEWYYOXIH7CIXPWCKOSRRDQRSDMXANCNFSM4B2QJCFQ .

louiepaguilar avatar Nov 02 '19 03:11 louiepaguilar

Okay, so if it's working on your local machine but not on the production. It's working because you are accessing your local network from your machine.

Here's the fix.

const httpServer = require('http').createServer();
const app = require('./app');

const ENV = process.env.NODE_ENV || 'development';

global.io = require('socket.io').listen(httpServer);

httpServer.listen(3002, 'PRODUCTION-URL-HERE', () => {
  console.info(`Socket server started on ${PRODUCTION-URL-HERE}:${3002} (${ENV})`);
});

const src = app;

module.exports = src;

And the frontend:

componentDidMount() {
        let socket = io('PRODUCTION-URL-HERE');

        socket.on('notification', data => {
            this.setState({
                notifications: data
            })
        })
    }

Elvis339 avatar Mar 01 '20 16:03 Elvis339

hello

liyerun avatar Nov 22 '22 13:11 liyerun

anyone?

Notekey avatar Sep 04 '23 02:09 Notekey

@Notekey hi! You are commenting on an old issue, please open a new issue with all necessary details. Thanks!

Please also check our tutorial: https://socket.io/docs/v4/tutorial/introduction

darrachequesne avatar Sep 13 '23 10:09 darrachequesne