Reputation: 21
I'm using flask and socketio to build a server-client comms system. A simplified version of it is below, which replicates the issue I'm having. Basically, the client can connect to the server and can send messages and receive acknowledgements back, however the messages don't seem to trigger the handler functions on the server. In the example below, I'm expecting the sever to have 'RECEIVED: {} {}' in the logs, as it should call the handle_message
method, but there's nothing, despite it specifically saying it's received the 'test' event. I've tried a few different iterations and versions to try to work out what's happening, but I have no idea what's causing the issue. Any help you can provide would be greatly appreciated! Thanks!
test_simple.py:
import sys
import time
from flask import Flask
from flask_socketio import SocketIO
import socketio as sio
from socketio import Client
app = Flask(__name__)
socket = SocketIO(app, engineio_logger=True, logger=True)
@socket.on('test') ## try decorator as .on_event isn't working
def handle_message(*args, **kwargs):
print('RECEIVED: ', args, kwargs)
app.logger.error(f'RECEIVED: {args}, {kwargs}')
socket.emit('reply', 'hello client')
return 'hello client'
def message_callback(*args, **kwargs):
print('CALLBACK: ', args, kwargs)
def startup_server():
#app = Flask(__name__)
#socket = SocketIO(app, cors_allowed_origins="*",
# engineio_logger=True, logger=True)
#socket.on_event('test', handle_message) # this doesn't work either
#socket.on_event('message', handle_message)
socket.run(app, host='127.0.0.1', port=8001, debug=True,
use_reloader=False)
def startup_client():
socket = Client(logger=True, engineio_logger=True)
socket.connect('http://127.0.0.1:8001')
socket.on('reply', message_callback)
socket.emit('test', data='hello world', callback=message_callback)
time.sleep(5)
#socket.send(data='hello message', callback=message_callback)
#time.sleep(5)
socket.disconnect()
if __name__ == '__main__':
if sys.argv[1] == 'predictor':
startup_client()
else:
startup_server()
Server logs:
python3 test_simple.py aggregator
Server initialized for gevent.
54Pavf_I3leodKsoAAAA: Sending packet OPEN data {'sid': '54Pavf_I3leodKsoAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
54Pavf_I3leodKsoAAAA: Received request to upgrade to websocket
54Pavf_I3leodKsoAAAA: Upgrade to websocket successful
54Pavf_I3leodKsoAAAA: Received packet MESSAGE data 0
54Pavf_I3leodKsoAAAA: Sending packet MESSAGE data 0{"sid":"xH33UEQVeWzMXauFAAAB"}
54Pavf_I3leodKsoAAAA: Received packet MESSAGE data 21["test","hello world"]
received event "test" from xH33UEQVeWzMXauFAAAB [/]
### << expecting 'RECEIVED: {} {}' here >> ###
54Pavf_I3leodKsoAAAA: Sending packet MESSAGE data 31["",400]
Client logs:
python3 test_simple.py predictor
Server initialized for gevent.
Attempting polling connection to http://127.0.0.1:8001/socket.io/?transport=polling&EIO=4
Polling connection accepted with {'sid': '54Pavf_I3leodKsoAAAA', 'upgrades': ['websocket'], 'pingTimeout': 20000, 'pingInterval': 25000}
Engine.IO connection established
Sending packet MESSAGE data 0
Attempting WebSocket upgrade to ws://127.0.0.1:8001/socket.io/?transport=websocket&EIO=4
WebSocket upgrade was successful
Received packet NOOP data
Received packet MESSAGE data 0{"sid":"xH33UEQVeWzMXauFAAAB"}
Namespace / is connected
Emitting event "test" [/]
Sending packet MESSAGE data 21["test","hello world"]
Received packet MESSAGE data 31["",400]
Received ack [/]
CALLBACK: ('', 400) {} ## << expected to receive 'hello client' >> ##
## << no 'reply' event trigger >> ##
Upvotes: 1
Views: 2991
Reputation: 21
Solved it - thanks for your help Miguel! The issue was the package versions; I was using Flask 1.1.2 and Flask-SocketIO 4.3.1, which I hadn't realised was a separate package and I'd only tried updating Flask (and socketio-client, but I was already using the most update-to-date version of that). Once I'd updated to Flask 2.0.1 and Flask-SocketIO 5.1.0 as well, it suddenly started working as I'd expected it to. Now I feel dumb, haha
Upvotes: 1