rafaeliga

rafaeliga

Liveview + PubSub messages delay?

Hello there,

I have a Liveview that subscribes to a Pubsub topic and starts a Genserver when I click in a button.

The Genserver sends a message back to the Liveview using the Pubsub, those messages seems to only be processed after the map ends:

If I increase the data, from 1000 to 10000000, I see that some of messages being received before:

Is there a way to receive those message before?

I have created a repository to reproduce: GitHub - rafaeliga/liveview_pubsub_update: Sample project to test Liveview + Pubsub · GitHub.

Marked As Solved

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

Yes, but start is blocking handle_event, which blocks handle_info. start does not return until init returns. This means that handle_event doesn’t return until init returns, which means that the live view is unable to do any handle_info calls until init has published all of the messages.

EDIT: To elaborate further: A live view is a genserver, and a genserver is a single process. A single process can only run code linearly, and that means that a given callback from a genserver can only run one at a time. As long as handle_event is blocked, the genserver loop of the whole liveview process is blocked, which prevents any handle_info clause from running. If you change your broadcaster to do:

 def init(_params) do   
   send(self(), :broadcast)
   {:ok, nil}
 end

def handle_info(:broadcast, state) do
   Enum.map(1..1000000, fn datum ->
     Logger.info("process: #{datum}")
     
     Phoenix.PubSub.broadcast(LiveviewPubsubUpdate.PubSub, "import_live", {:message, datum, Timex.now()})
   end)
   
   Logger.info("Enum map finished")
  {:noreply, state}
end

You should see more of what you expect.

Also Liked

rafaeliga

rafaeliga

Thank you @benwilson512 !

The code below worked:

 def init(_params) do   
   send(self(), :broadcast)
   {:ok, nil}
 end

def handle_info(:broadcast, state) do

Then looking at the Genserver documentation, handle_continue did the job too:

  @impl true
  def init(_params) do
    {:ok, nil, {:continue, "continue"}}
  end
  
  @impl true
  def handle_continue("continue", state) do
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

You call start in handle_event. Start is not in another process, it’s in the liveview process. init is in another process, but start waits on init to return before it returns, that’s part of the standard genserver contract see:

iex(4)> defmodule Foo do
...(4)>   use GenServer
...(4)>
...(4)>   def start() do
...(4)>     GenServer.start(__MODULE__, nil)
...(4)>   end
...(4)>
...(4)>
...(4)>   def init(_) do
...(4)>     Process.sleep(1000)
...(4)>     {:ok, nil}
...(4)>   end
...(4)> end
iex(7)> :timer.tc(fn -> Foo.start() end)
{1000836, {:ok, #PID<0.145.0>}}

EDIT: Further reading

https://github.com/erlang/otp/blob/master/lib/stdlib/src/gen_server.erl#L74-L91

thiagomajesk

thiagomajesk

Oh, I see, that makes total sense! Thanks for the well-rounded explanation @benwilson512. I completely forgot that the GenServer process everything linearly like you mentioned (because it’s basically just a loop waiting for its time to process something). This definitely is what’s going on here.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29377 241
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30877 112
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement