sreyansjain

sreyansjain

Server Sent Events - https does not work: net::ERR_HTTP2_PROTOCOL_ERROR

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.

Marked As Solved

sreyansjain

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.

Also Liked

vfsoraki

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

Exadra37

Exadra37

Phoenix has a built-in tool for this:

mix phx.gen.cert

Check instructions at config/dev.exs:

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

vfsoraki

vfsoraki

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.

Last Post!

vfsoraki

vfsoraki

At some point in future, yes :smiley:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement