Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnecting from callback #262

Closed
ChaoticMind opened this issue May 12, 2016 · 3 comments
Closed

Disconnecting from callback #262

ChaoticMind opened this issue May 12, 2016 · 3 comments
Assignees

Comments

@ChaoticMind
Copy link

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)

@miguelgrinberg
Copy link
Owner

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.

@betterRunner
Copy link

@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.

@miguelgrinberg
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants