invisibleroads / socketIO-client

A socket.io client library for Python
http://pypi.python.org/pypi/socketIO-client
MIT License
447 stars 205 forks source link

Question about wait after emit #121

Closed brechmos closed 7 years ago

brechmos commented 8 years ago

Not an issue, just a question. I have been using socketIO-client for a while now and have found it to be a great implementation.

One thing that has always baffled me is why I need a socketIO.wait(seconds=1) after an emit?

In my more cavalier days I tried removing it thinking, "why wait a second?" but quite quickly found that removing it caused lots of issues with my socket.io server.

So, I am curious why that is required? Why a second? Why is it not built-in to the emit directly?

invisibleroads commented 7 years ago

Thanks for the compliment!

That's an interesting suggestion. I could integrate have emit call wait(seconds=0.1) and it might simplify code that uses this library.

On second thought, I think I should leave it up to the user to decide when to process events. In the current implementation, the emit method sends the packets to the server right away but does not process any responses from the server until the wait method is called.

I think I separated the wait method in order to let the user control when to process events. For example, the user might want to send a bunch of emits first before processing any events. This is especially important if some of the events involve callbacks to the server.

By forcing the user to process events from the server separately in a wait, we let the user control the sequence of initial emits in a reliable way. Subsequently calling wait activates the event-based portion, where timing and order can vary depending on the network.