rafaeliga

rafaeliga

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.

Showing Posts 1 to 10

Nicd

Nicd

The processes are running concurrently and there isn’t really a way to force the subscriber to process a message before the publisher can continue in a PubSub, I think. Looks like the sending is faster than receiving in this case (or the messages go slower through the PubSub system).

Anyway, why do you want the receiving to occur before? Is it leading to a time delay?

I think something like GenStage/Flow could provide backpressure, leading to blocking of the producer before consumers have had time to consume the items.

cevado

cevado

it’s not that sending it’s faster. what happens is that publishing the message won’t stop a process of running its reductions.

on your solution, what you want to be synchronous and what you want to be asynchronous?
The idea is to design the system to solve the thing the way you need. PubSub is a asynchronous solution.

rafaeliga

rafaeliga OP

My use case is showing a CSV import / insert data in database in real time.

cmo

cmo

What your example shows is that a) it is extremely quick to map through a list when you do no work in the map function and b) pubsub works. You can probably move on to the next stage.

You could delay processing the next chunk until after the message is received by calling the liveview instead of broadcasting, but I don’t imagine you want to do that in practice.

rafaeliga

rafaeliga OP

Sorry if my initial post isnt clear enough, let me try with more data:

There is a time difference between the Pubsub send message and Liveview receiving.

That time increases if I send more messages(more data):

dev
100: ~16ms
1000: ~50ms
100000: starts at: ~700ms, ends at: ~3000ms
1000000: starts at: ~7000ms, ends at: 38000ms

prod(fly.io)
1000: ~180ms
1000000: crashed

Why do we have this difference if I have more data? Its related to the Pubsub or the Liveview on receiving these messages?

cevado

cevado

Because message passing between process is not a synchronous procedure.

  • process A sends a message to the message box of process B.
  • scheduler alocate time for any process that has something to do.
  • process B starts processing messages on his message box.

PubSub publishing message is a non-blocking operation, just like doing a cast with a GenServer.
Since publishing is non-blocking, the process A will keep doing whatever his doing until the scheduler stop it from running(in your case, keep publishing messages until it finishes starting).

The delays you’re experiencing in your test is not particular to any implementation of the PubSub or Liveview but instead of how you choosed to implement your synthetic load.

thiagomajesk

thiagomajesk

Hi @cevado! I think there’s some misunderstanding between what @rafaeliga wants and what you are explaining. I don’t think he is expecting to process anything synchronously.

If I understand correctly, what @rafaeliga meant is that there’s some considerable delay from sending to receiving the message; this is nothing to do with the concurrency model per se.

You mentioned reductions and this made me think that perhaps, the LiveView process’s mailbox is getting too many messages and the delay he’s experiencing is the delta between processing the messages.

However, based on the data he provided, it seems that there’s a considerable delay between sending the message and receiving it, even with a low amount of messages to process.

Curiously enough, from what I’ve heard about Elixir/Phoenix in the past, I’d expect that broadcasting messages would be a little bit more performant than that. But perhaps, this is a question on how to better structure the message passing between the processes (perhaps batching it or something).

cevado

cevado

please look at the code provide…
code that starts the “background processing”:

def handle_event("process", _params, socket) do
    LiveviewPubsubUpdate.Import.start()
    
    {:noreply, socket}
  end

the “background processing”:

def start() do
   GenServer.start(__MODULE__, nil)
 end

 @impl true
 def init(_params) 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")
   
   {:ok, nil}
 end

when LiveviewPubsubUpdate.Import.start() is called it is a synchronous process, so start() will release the live view only when the init callback finishes running.
the init callback in the GenServer is that part that publishes.

thiagomajesk

thiagomajesk

@cevado could you elaborate what you meant by this statement? Bear in mind that even though LiveviewPubsubUpdate.Import.start() is synchronous code, the Phoenix.PubSub.broadcast call is not. That is, messages arrive in the LiveView before the synchronous code finishes processing, which does not seem to be the problem IMHO.

The main question seems to be that after a message is dispatched to the LiveView, there’s some delay before it actually gets processed.

I think that if LiveView was receiving and processing its events fast enough, this perceived delay would not exist. I might be missing something here, but it doesn’t seem to be a problem about concurrency.

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.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New
webofbits
Aludel - LLM Evaluation Workbench Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews