Repository navigation
Add Server Sent Events #197
Description
Description
There is no mechanism in place for clients to receive updated data from the web server besides polling. For example, our Kiosk fetches data periodically from our web server, which means any new data is unknown until the next polling cycle and if there is no new data the Kiosk will fetch and process data it already has, which is wasteful.
FastAPI has a feature called Server-Sent Events which allows the web server to hold a list of subscribers and publish data updates to them. This should cut down the traffic to our web server since clients would have an option to just listen for updates from the server.
Implementation Details
At a high level I think this is what needs to be implemented:
- Endpoint(s) to subscribe, so the web server knows who it should send updates to. This should require
execor higher privilege, or the Kiosk. - Triggers that cause the web server to send notifications to its subscriber list that a certain data source has been updated. This would need to be added to
POST,PUT,PATCH, andDELETEendpoints or the database would be the one to send notifications. The database seems like a better place to set triggers on. - Notification would look something like
data: "<data source name>", "event": "update". Intermittent keep alive pings, to ensure connections aren't closed.Cache-Control: no-cacheto prevent browser caching of stream data.X-Accel-Buffering: noto prevent buffering in proxies (like Nginx, which we use).
Note: 4, 5, and 6 are already handled by FastAPI
Data sources
These are the data sources users can get updates to:
- Events
- Media
- Users
- Officers
- Elections
- Nominees
- Candidates
- Honorary
Things to test
- When clients disconnect are subscriptions cleaned up automatically or does the client need to send a message to close?
- If the server crashes what happens to these subscriptions on the clients?
Other notes
- This would probably be annoying to implement onto every single modification endpoint, so we could look into a middleware handling this.
Resources
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsReady
Activity