A simple demo of Server-sent Events (SSE) using Cowboy and Plug in Elixir.
Just a learning thing. Much help from these gists: jeregrin, rbishop
TL;DR is that it's a simple technology for streaming data/events to a client over a long running HTTP connection.
Server-Sent Events Specification on w3
retry: <milliseconds>
: can be used to configure the client's reconnection time(?)id: <id>
: before adata:
line associates the data with the id, clients may keep track of that id and in case of a reconnection can add a headerLast-Event-ID
which then could be used by the server to re-send everything that was missed.event: <name>
: can be used to associate the nextdata:
line with a category/type of event.- To make sure the connection is not closed by for example proxies, firewalls etc I could send heartbeats. (For example just a line starting with
:
, since those should be ignored by the client)
Make sure you have Elixir installed.
mix deps.get
mix run --no-halt
- The demo app should now be running and listening on
localhost:4000
. On/events
you can get an infinite stream of events. Ex:curl localhost:4000/events