Gedward Romo
Gedward Romo

Reputation: 351

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"

Upvotes: 3

Views: 10116

Answers (4)

Parvat R
Parvat R

Reputation: 846

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.

Upvotes: 2

ram
ram

Reputation: 1

Just install requests, sometimes it doesn't work if we haven't imported requests. It worked for me so: pip install requests

Upvotes: -1

Nas
Nas

Reputation: 27

Ran into the same thing. For me it seemed to be an issue in Windows 10: it requires

sio.connect('http://192.168.178.18:8080')

to display the exact IP-address of your computer. Not 'localhost' or anything. Then it connected smoothly.

Upvotes: 0

Gedward Romo
Gedward Romo

Reputation: 351

I solved with origins parameter:

const io = require('socket.io')(server,{pingTimeout: 0, pingInterval: 500, origins: '*:*'});

Upvotes: 1

Related Questions