Flagsmith/flagsmith-python-client

Implement Real-Time Functionality

Closed this issue · 1 comments

The Python client should support our real-time functionality via SSE. When using local evaluation mode, the client should listen for events on the stream and update its local cache of data if the value if the value of updated_at that was provided in the stream message is more recent than the last one received (and stored) from the X-Flagsmith-Document-Updated-At header received from the relevant API endpoint.

We should start by implementing this for local evaluation, since we already maintain a local cache of the environment document in this instance.

With regards to remote evaluation modes, we will need to implement a caching interface that stores the Flags class from the latest response from the relevant endpoint, and retrieves the flags again if it receives an event on the SSE stream. For now, this cache interface only needs to support a local memory cache, but we should make the interface generic to support other options.

Thinking something like this:

# define use_stream (or similar) in the __init__ method on the client. 

def connect(self)
    while self.use_stream:
        try: 
            stream = requests.GET(stream_url, stream=True)
            
        except requests.ConnectionError:
            reconnect()