1

I am using this Python socket.io client library. This is part of the python code which is used to instantiate and connect to the socket io server:

import socketio

sio = socketio.Client()
logger.info('Created socketio client')
sio.connect('https://server.com/socket.io/')

@sio.event
def connect():
    logger.info('connected to server')

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

The logger message 'Created socketio client' is printed out but it does not show the 'connected to server' message. However, during manual closing of the server connection, the 'disconnect' callback is activated... What went wrong here? I am using a nginx proxy here fyi.

Eugene
  • 1,013
  • 1
  • 22
  • 43

1 Answers1

2

Well, for starters you are connecting before the handlers are defined. You are also not doing anything after the connection. Try this version instead:

import socketio

sio = socketio.Client()
logger.info('Created socketio client')

@sio.event
def connect():
    logger.info('connected to server')

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

sio.connect('https://server.com/socket.io/')
sio.wait()

The wait() call at the end ensures that the main thread blocks until the client is disconnected.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Hi, By referring your answer I am trying to implement the same but facing some connection issue. I have created a question here https://stackoverflow.com/questions/73331659/python-socketio-connection-refused-by-the-server Is it possible to check it? – Prabhakaran Aug 12 '22 at 08:57