Flagsmith / flagsmith

Open Source Feature Flagging and Remote Config Service. Host on-prem or use our hosted version at https://flagsmith.com/
https://flagsmith.com/
BSD 3-Clause "New" or "Revised" License
4.62k stars 353 forks source link

Implement SSE for all SDKs #1498

Open dabeeeenster opened 1 year ago

dabeeeenster commented 1 year ago

Client Side

Server Side

Note: this is a parent ticket, when working on the implementation in a given SDK, there should be a ticket (referenced here) with relevant acceptance criteria defined.

Requirements

On initialisation, the client must open an SSE connection to https://realtime.flagsmith.com/sse/environments/{environment key}/stream.

Whenever a flag changes (or an entity changes which might affect the value of a flag, e.g. a segment) in the Flagsmith Dashboard, an event is sent via the stream. The event will have a type of environment_updated and a body that looks something like {"updated_at": 1689172003.899101} where the value of "updated_at" is an iso timestamp of when the environment was last updated.

Each call to /flags, /identities and /environment-document also includes the X-Environment-Updated-At response header which contains the same value. The clients can use this to initially set the state for this value and then continue to update it by listening to the SSE stream.

The client is expected to maintain a cache of the last time the environment was updated and, when it receives an event on the SSE stream, to determine if the environment was updated more recently and if so, to call the relevant endpoint to retrieve the flags for the environment / identity and update it's local state wherever necessary.

See the JS SDK code here for reference.

matthewelwell commented 8 months ago

Some implementation assistance for .NET: https://makolyte.com/event-driven-dotnet-how-to-consume-an-sse-endpoint-with-httpclient/