Skip to content

Add Server Sent Events #197

Open

Description

@jbriones1

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:

  1. Endpoint(s) to subscribe, so the web server knows who it should send updates to. This should require exec or higher privilege, or the Kiosk.
  2. 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, and DELETE endpoints or the database would be the one to send notifications. The database seems like a better place to set triggers on.
  3. Notification would look something like data: "<data source name>", "event": "update".
  4. Intermittent keep alive pings, to ensure connections aren't closed.
  5. Cache-Control: no-cache to prevent browser caching of stream data.
  6. X-Accel-Buffering: no to 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestkioskFor issues related to the kiosk, but not in that repository

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions