You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to call disconnect() after emitting a message to a socketio client:
emit("log message", {"data": "Failed to process request"}, callback=disconnect)
However, this doesn't work, and I get a RuntimeError: working outside of application context.
Initially, I was trying to simply emit the payload and then calling disconnect() immediately afterwards, like so:
emit("log message", {"data": "Failed to process request"})
disconect()
However, that doesn't work, and the client seems to disconnect before emitting the message. Well, it makes sense that the order is not guaranteed in that case and that is a race condition. So I figured the callback method should work. Is there another recommended way to do this?
Note that after getting the RuntimeError, the socket does disconnect :p (due to the exception, I assume)
The text was updated successfully, but these errors were encountered:
The disconnect function can only be called inside an event handler as it needs a context, you cannot use it as a callback.
I will keep this issue as a feature request, which is to set up a context before invoking callback functions.
The easiest way to do what you want with the current version is to have the client send an event that triggers the disconnection once it has received the data. So basically it is a callback solution implemented by hand.
@miguelgrinberg, I got a little confused about this strategy you proposed, that is:
If the client do not send back an event to me, then I could be impossiable to disconnect it forwardly, right? That sounds not reasonable in some application, chould you please explain more about this? Thank you very much.
What I proposed is a workaround. The code given in the issue is never going to work, the disconnect function is really not a function, it's a method, so that is not appropriate as a callback. There are other options you can use, but I really fail to understand what's the point of disconnecting a WebSocket connection right after some data is exchanged. That is what HTTP does, why use WebSocket or SocketIO if all you want is send some data and then hang up?
I'd like to call
disconnect()
after emitting a message to a socketio client:However, this doesn't work, and I get a
RuntimeError: working outside of application context
.Initially, I was trying to simply emit the payload and then calling
disconnect()
immediately afterwards, like so:However, that doesn't work, and the client seems to disconnect before emitting the message. Well, it makes sense that the order is not guaranteed in that case and that is a race condition. So I figured the callback method should work. Is there another recommended way to do this?
Note that after getting the
RuntimeError
, the socket does disconnect :p (due to the exception, I assume)The text was updated successfully, but these errors were encountered: