Generate a new Elixir project using mix
and add cowboy
and plug
as dependencies in mix.exs
:
defp deps do
[
{:cowboy, "~> 1.0.0"},
{:plug, "~> 0.8.1"}
]
end
Then add the sse.ex
file below to lib/
and run the code with mix run --no-halt lib/sse.ex
. Point your browser to http://localhost:4000
and you're done.
If you don't feel like writing your own EventSource JavaScript, you can use the example in index.html below that comes from Cowboy. Just create a priv/static
directory inside of your project.
Looks very good. An example how to connect to the server from an Elixir client would make this even better and complete.