socketio / socket.io

Realtime application framework (Node.JS server)
https://socket.io
MIT License
60.65k stars 10.09k forks source link

net::ERR_CONNECTION_REFUSED #2431

Closed sametaylak closed 7 years ago

sametaylak commented 8 years ago

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

mehmetakifalp commented 8 years ago

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 notifications@github.com:

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

sametaylak commented 8 years ago

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 :/

mehmetakifalp commented 8 years ago

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 notifications@github.com:

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

darrachequesne commented 7 years ago

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).

louiepaguilar commented 7 years ago

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. =(

hienpham2tiki commented 7 years ago

@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');

louiepaguilar commented 7 years ago

@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!

PiroozMB commented 4 years ago

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.

louiepaguilar commented 4 years ago

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

On Sat, 2 Nov 2019, 4:02 AM PiroozMB notifications@github.com 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 .

Elvis339 commented 4 years ago

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
            })
        })
    }
liyerun commented 1 year ago

hello

Notekey commented 10 months ago

anyone?

darrachequesne commented 10 months ago

@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