Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error socketio library: "socketio.exceptions.ConnectionError: Connection refused by the server"

I am trying to connect to a socket that I have configured in NodeJS, from anywhere outside the server if I can connect to the Socket, but when trying to connect from the same server it shows the message: "socketio.exceptions.ConnectionError: Connection refused by the server"

The port that I have configured is 8085 I have tried adding: - http://localhost: 8085 - https://localhost: 8085 - http://127.0.0.1: 8085 - https://127.0.0.1: 8085 - *: 8085 - 192.168.4.7:8085 - PublicIP: 8085

import socketio
sio = socketio.Client()
sio.connect('https://localhost:8085')

When I connect from outside the server it allows me to interact with the socket. The problem is the local server because it immediately shows me the message "Connection refused"

like image 697
Gedward Romo Avatar asked Feb 27 '26 12:02

Gedward Romo


2 Answers

When taking a look at the socketio's docs, it is shown that there are two or more instances running. One for the server side and others for the client side. Let the server file run the server and connect to the server with the client files. An example is given below:


server.py:

# using eventlet, visit the docs for more info
import eventlet
import socketio

sio = socketio.Server()
app = socketio.WSGIApp(sio, static_files={
    '/': {'content_type': 'text/html', 'filename': 'index.html'}
})

@sio.event
def connect(sid, environ):
    print('connect ', sid)

@sio.event
def my_message(sid, data):
    print('message ', data)

@sio.event
def disconnect(sid):
    print('disconnect ', sid)

if __name__ == '__main__':
    # change the 5000 to any port you want
    # leave the 'localhost' empty string to run on your IP
    eventlet.wsgi.server(eventlet.listen(('localhost', 5000)), app)

client.py:

import socketio

sio = socketio.Client()

@sio.event
def connect():
    print('connection established')

@sio.event
def my_message(data):
    print('message received with ', data)
    sio.emit('my response', {'response': 'my response'})

@sio.event
def disconnect():
    print('disconnected from server')

sio.connect('http://localhost:5000/')
sio.wait()

Then, run the server.py first:

C:\User\new>py server.py
(12080) wsgi starting up on http://127.0.0.1:5000

Then run client.py:

C:\User\new>py clinet.py
connection established

And your socket file is running.

You will feel easy with this process if you have already experienced with the socket.io in JavaScript. Because that is the perfect and most easy way to get started and learn socket.io.

like image 185
Parvat . R Avatar answered Mar 02 '26 00:03

Parvat . R


I solved with origins parameter:

const io = require('socket.io')(server,{pingTimeout: 0, pingInterval: 500, origins: '*:*'});
like image 40
Gedward Romo Avatar answered Mar 02 '26 02:03

Gedward Romo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!