Flask-SocketIO: why the client can not receive the emit data during connecting?
@socketio.on("connect", namespace="/test")
def test_connect():
emit("my_response", {"data": "Connected", "count":0})
in the test demo, this works well, and i can see the data {“data”: “Connected”, “count”:0} in the webbrowser.
but if i use it like following:
@socketio.on("connect", namespace="/test")
def test_connect():
emit("my_response", {"data": "Connected", "count":0})
## add here ##
return False
then in the browser, it will try to connect, but the strange place is that i can not see the data {“data”: “Connected”, “count”:0} in the webbrowser console, i have used ipdb to trace, and it really has sent the data. so i am confused why client can not recieve the response data? can you help me? thanks
this is the pic without return False

this is the pic with return False:

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 16 (7 by maintainers)
Okay, then this is wrong. You have two options to do that:
Add the
always_connect=Truesetting in theSocketIOconstructor. This will fully accept the connection before the connect handler is invoked, so you can emit freely. If you returnFalse, then adisconnect()will be issued to close the connection.Instead of returning
False, raise aConnectionRefusedErrorexception, which takes an argument where you can pass an error message back to the client: