sreyansjain
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.
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sreyansjain
Whole thing works well over https when I use a real domain and a valid certificate. The issue seems to be there only with localhost.
I am still trying to figure out how to make it work over localhost using https.
vfsoraki
For local certificates, I use makecert to generate valid certificates for local domains. I am on Linux, but this or similar tools should be available for other OSes.
I then add my local domains to
/etc/hostsand I also have a local Nginx configured to serve my local projects using their respective local domains.Should be easy to set up. If you’re having a problem, reply here.
sreyansjain
Thanks. I will try it out.
hauleth
If you are using systemd-resolved then you can use
*.localhostwithout editing/etc/hosts(whole domain should resolve to loopback address).vfsoraki
Nice to know. I am indeed using it.
But I’m not using
*.localhost.Exadra37
Phoenix has a built-in tool for this:
Check instructions at
config/dev.exs:vfsoraki
Is is locally trusted? I can’t find it if docs mention this anywhere.
Exadra37
No, you need to allow the certificate when the browser prompts you.
vfsoraki
mkcertsmakes them trusted locally, which is a nice thing.Exadra37
But also creates certificates for
example.comand I don’t like it overriding a website that exists:I usually prefer Bash for this type of tasks, and I have wrote a script to do it. I use it in my docker builds or from my localhost host when needed.