Project

General

Profile

Idea #7658

Updated by Brett Smith about 8 years ago

In order to start having long-running tasks that rely on websockets events to trigger state changes, we need to ensure that the Python websockets client will automatically reconnect when there is an unexpected disconnect. 

 h2. Acceptance criteria Suggested implementation: 

 * The public interface of EventClient does not change. # Create a new WebsocketEventClient class that doesn't inherit from WebSocketClient 
 * Clients that use the public interface of EventClient are automatically get reconnected to websockets.    In other words, the # WebsocketEventClient takes @on_event@ callback is called with an uninterrupted stream of events, even if in the underlying client constructor and has to connect to the websockets server multiple times to provide it. 
 * Existing tests should continue to pass without modifying the following public methods called and the results they return. API: @subscribe()@, @unsubscribe()@, @close()@ 
 * Add a test that forcibly disconnects the client and asserts that it automatically reconnects and passes in the correct last_log_id. 

 h2. Suggested implementation 

 # The current EventClient class becomes an internal class (_EventClient) 
 # Add a @on_closed()@ callback to _EventClient. 
 # Create a new EventClient class that inherits from _EventClient. 
 # EventClient takes @on_event@ in the constructor and has the following public API: @subscribe()@, @unsubscribe()@, @close()@ 
 # EventClient WebsocketEventClient creates an instance of _EventClient and sets callbacks on_event() and on_closed() to itself 
 # When subscribe() and unsubscribe() are called EventClient, WebsocketEventClient, record in an array and then forward to _EventClient 
 # When events are called from _EventClient, record the sequence number "id" in last_log_id and forward them to the real on_event() handler. 
 # If close() is called by user, set a flag 
 # If on_closed() event is called on EventClient, WebsocketEventClient, but close flag is false, disconnect from old _EventClient EventClient object, create a new _EventClient object, and replay the subscriptions with last_log_id set  

Back