polypush135

polypush135

Trying to better understand broadway

Was reading How to Use Broadway in Your Elixir Application | AppSignal Blog
and wanted to get a better understanding of how the incoming messages can be triggered and managed.

Right out the box with this tut, it uses a WorldTemp.CityProducer example that I have a hard time understanding. There are two cases of handle_demand, one is for when demand > length(state) the other for when this is not true.

First: What does demand here represent? It starts with a default value of 10. Where does this value come from and what does it represent? What should I read to get a betting understanding of this value?

Second: The city list function is a static list. What if I wanted to do an ecto query and get a list of rows that should be processed. Is this where that request would look up said rows?

I guess so far the producer is the most confusing part to me.

Edit: I think I figured out my disconnect. This module is a GenStage. GenStage — gen_stage v1.3.2 and not directly related to Broadway.
Time to go learn some GenStage I guess.

First 4 of 4 Posts! Switch mode

akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

Author of the article here and good questions!

The reason for the two different handle_demand functions (one with the guard and one without) is because our GenStage producer effectively creates an infinite list of cities. That first function with the guard ensures that there are enough cities in the list to satisfy the incoming demand by adding more items to the list. The demand in this case represents what the downstream Broadway consumers are requesting from this producer.

While you can use GenStage as a producer, in production you most often would use Broadway with something like RabbitMQ or Kafka. I chose GenStage as a producer for that article mostly to keep the length of the article within reason. I have a more in depth 2-part series tutorial on my personal blog that leverages RabbitMQ, Broadway, and Ecto:

polypush135

polypush135

Say I wanted to query a list of cities using Echo via fetching a list of cities from the repo on an interval. Where would you start with something like that? In my case there could be times there are no cities and othertimes the db is full of cities. Is this a correct use case for this?

akoutmos

akoutmos

Author of Build a Weather Station with Elixir and Nerves

I did something roughly similar to what you are describing in the 2 part series. I have a GenServer that publishes messages to RabbitMQ when certain conditions are met. You could easily modify my GenServer to make Ecto queries on a regular interval instead though. With the data that gets fetched from the DB, you can publish individual messages to RabbitMQ for each item of work that needs to take place. On the other side of that, Broadway will read those messages and process them.

polypush135

polypush135

Thanks for the pointers, that helps.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement