How to build server sent events with Phoenix?

As far as I’ve seen yet there is no built-in way to do SSE in Phoenix, and as the Phoenix channel API covers that use-case and more via websocket/longpolling, most have not seen a need.

However, there is a good use-case for SSE and it still can be done in Phoenix/Plug.

First, you will want to add the mimetype of "text/event-stream" as an acceptable mimetype at whatever router paths you want.

Second, you will need to send data via normal HTTP chunked encoding if I remember right. I know how to do this in Cowboy, and I know Phoenix/Plug does it via longpolling channels, but I’ve not done it in Phoenix/Plug itself so maybe @josevalim can elaborate on how it is done as a quick googling failed me? ^.^

And the above might not even be necessary if Phoenix Channels already have a built-in way to emulate Server-Side Events, I’ve not checked yet. :slight_smile:

Edit: Just as I send this I find a plug way to send chunked data, apparently it is just the chunk/2 call: A super simple Elixir server for sending Server Sent Events to the browser. · GitHub ^.^

Some kind of one-way channel interface would be a better way if it exists already though. :slight_smile: