# Server Sent Events - https does not work: net::ERR\_HTTP2\_PROTOCOL\_ERROR

**URL:** <https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238>\
**Category:** Questions\
**Tags:** phoenix, sse\
**Created:** [April 27, 2021, 7:04am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238 "2021-04-27T07:04:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![sreyansjain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sreyansjain/32/16464_2.png) [@sreyansjain](https://forum.elixirforum.com/u/sreyansjain)\
**Post date:** [April 27, 2021, 7:04am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/1 "2021-04-27T07:04:50Z")

</div>

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

```elixir
config :mime, :types, %{
  "text/event-stream" => ["sse"]
}

```

In my controller I have

```elixir
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

```elixir
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

```elixir
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

```elixir
<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](https://code.krister.ee/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](https://localhost:8077/notification) net::ERR\_HTTP2\_PROTOCOL\_ERROR

Request your kind help.

---

<div class="post-metadata">

**Author:** ![sreyansjain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sreyansjain/32/16464_2.png) [@sreyansjain](https://forum.elixirforum.com/u/sreyansjain)\
**Post date:** [April 27, 2021, 9:38am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/2 "2021-04-27T09:38:17Z")

</div>

**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.

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [April 27, 2021, 10:27am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/3 "2021-04-27T10:27:41Z")

</div>

For local certificates, I use [makecert](https://github.com/FiloSottile/mkcert) 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/hosts` and 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.

---

<div class="post-metadata">

**Author:** ![sreyansjain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/sreyansjain/32/16464_2.png) [@sreyansjain](https://forum.elixirforum.com/u/sreyansjain)\
**Post date:** [April 27, 2021, 11:26am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/4 "2021-04-27T11:26:18Z")

</div>

Thanks. I will try it out.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [April 27, 2021, 11:54am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/5 "2021-04-27T11:54:52Z")

</div>

> [@vfsoraki](#):
>
> I then add my local domains to `/etc/hosts`

If you are using systemd-resolved then you can use `*.localhost` without editing `/etc/hosts` (whole domain should resolve to loopback address).

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [April 27, 2021, 12:08pm UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/6 "2021-04-27T12:08:26Z")

</div>

Nice to know. I am indeed using it.

But I’m not using `*.localhost`.

---

<div class="post-metadata">

**Author:** ![Exadra37](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/exadra37/32/9183_2.png) [@Exadra37](https://forum.elixirforum.com/u/Exadra37)\
**Post date:** [April 27, 2021, 2:55pm UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/7 "2021-04-27T14:55:55Z")

</div>

> [@vfsoraki](#):
>
> For local certificates, I use [makecert](https://github.com/FiloSottile/mkcert) to generate valid certificates for local domains. I am on Linux, but this or similar tools should be available for other OSes.

Phoenix has a built-in tool for this:

```elixir
mix phx.gen.cert

```

Check instructions at `config/dev.exs`:

```elixir
# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
# mix phx.gen.cert
#
# Note that this task requires Erlang/OTP 20 or later.
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
# https: [
# port: 4001,
# cipher_suite: :strong,
# keyfile: "priv/cert/selfsigned_key.pem",
# certfile: "priv/cert/selfsigned.pem"
# ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.

```

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [May 4, 2021, 10:03am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/8 "2021-05-04T10:03:47Z")

</div>

Is is locally trusted? I can’t find it if docs mention this anywhere.

---

<div class="post-metadata">

**Author:** ![Exadra37](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/exadra37/32/9183_2.png) [@Exadra37](https://forum.elixirforum.com/u/Exadra37)\
**Post date:** [May 4, 2021, 10:28am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/9 "2021-05-04T10:28:10Z")

</div>

> [@vfsoraki](#):
>
> Is is locally trusted?

No, you need to allow the certificate when the browser prompts you.

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [May 4, 2021, 11:18am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/10 "2021-05-04T11:18:03Z")

</div>

`mkcerts` makes them trusted locally, which is a nice thing.

---

<div class="post-metadata">

**Author:** ![Exadra37](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/exadra37/32/9183_2.png) [@Exadra37](https://forum.elixirforum.com/u/Exadra37)\
**Post date:** [May 4, 2021, 11:35am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/11 "2021-05-04T11:35:54Z")

</div>

> [@vfsoraki](#):
>
> `mkcerts` makes them trusted locally,

But also creates certificates for `example.com` and I don’t like it overriding a website that exists:

 ![Screenshot from 2021-05-04 12-29-19](https://forum.elixirforum.com/uploads/default/original/3X/6/9/69971b614b1d29d512e87820cca5b4aca568c75b.png)

I usually prefer Bash for this type of tasks, and I have wrote a [script](https://gitlab.com/exadra37-bash/docker/bash-scripts-for-docker-builds/-/blob/bbc8647cc491b49206631acdc18b087126f00aaa/scripts/custom-ssl/operating-system/create-self-signed-domain-certificate.sh) to do it. I use it in my docker builds or from my localhost host when needed.

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [May 4, 2021, 11:55am UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/12 "2021-05-04T11:55:19Z")

</div>

You’re free to not use `example.com`. I don’t use it. I use a TLD of my own, and I don’t have any problems since that TLD will never exist in the outer world.

---

<div class="post-metadata">

**Author:** ![hauleth](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/hauleth/32/18942_2.png) [@hauleth](https://forum.elixirforum.com/u/hauleth)\
**Post date:** [May 4, 2021, 8:39pm UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/13 "2021-05-04T20:39:43Z")

</div>

> [@vfsoraki](#):
>
> since that TLD will never exist in the outer world.

Unless it is `.example`, `.invalid` or `.localhost` and has more than 2 characters, then I need to disappoint you - it may exist (at some point in future) in the outer world.

---

<div class="post-metadata">

**Author:** ![vfsoraki](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/vfsoraki/32/11605_2.png) [@vfsoraki](https://forum.elixirforum.com/u/vfsoraki)\
**Post date:** [May 4, 2021, 8:53pm UTC](https://forum.elixirforum.com/t/server-sent-events-https-does-not-work-net-err-http2-protocol-error/39238/14 "2021-05-04T20:53:30Z")

</div>

At some point in future, yes 😃
