Hello,
I am trying to get server sent events to work with Phoenix. And I can get it to work successfully over http.
Following are the steps i followed -
Add mime type to config.exs
config :mime, :types, %{
"text/event-stream" => ["sse"]
}
In my controller I have
defmodule MyAppWeb.NotificationController do
use MyAppWeb, :controller
# alias Phoenix.PubSub
def index(conn, _params) do
conn =
conn
|> put_resp_header("Cache-Control", "no-cache")
|> put_resp_header("connection", "keep-alive")
|> put_resp_header("Content-Type", "text/event-stream; charset=utf-8")
|> put_resp_header("Access-Control-Allow-Origin", "*")
|> send_chunked(200)
IO.puts("===============")
PubSub.subscribe(
self(),
:notification
)
|> IO.inspect()
IO.puts("===============")
IO.inspect(self())
sse_loop(conn, self())
end
defp sse_loop(conn, pid) do
# receive is what stops the router from processing
# and waits for an event to come in
receive do
# Send updates when :cagg is finished.
{:notification, :done} ->
# Fetch notification
IO.inspect("*************************")
# Send update.
chunk(conn, "data: message\n\n")
# Wait for next publish.
sse_loop(conn, pid)
# Stop SSE if this conn is actually down.
# Ignore other processes finishing.
# Notice the "^" in front of pid.
{:DOWN, _reference, :process, ^pid, _type} ->
nil
# Don't stop SSE because of unrelated events.
other ->
sse_loop(conn, pid)
end
end
end
In the router I have
get "/notification", NotificationController, :index
Also need to set idle_timeout for cowboy to infinity otherwise it times out after 60secs
So in the dev.exs file I have
http: [port: 8066, compress: true, protocol_options: [idle_timeout: :infinity]],
https: [
port: 8077,
compress: true,
cipher_suite: :strong,
keyfile: "priv/keys/localhost.key",
certfile: "priv/keys/localhost.cert",
protocol_options: [idle_timeout: :infinity]
],
In the template I have simple javascript for SSE
<script>
let eventSource = new EventSource("/notification");
eventSource.onmessage = function(event) {
console.log("New message", event.data);
};
</script>
I got great help from this blog - Server Sent Events with Elixir
Everything works over http. However for https it doesn’t work.
I get this error
GET https://localhost:8077/notification net::ERR_HTTP2_PROTOCOL_ERROR
Request your kind help.