deerob4

deerob4

Moving from GenEvent to GenStage

I’m writing a quiz system using Elixir. Every time a user chooses an answer to a question, an %AnswerPacket{} struct is created, containing information on the answer. This packet is then processed by two GenServers, ScoreServer and AnswerServer, both of which have an update/2 function, taking the server pid and the packet.

Currently, I’m implementing this using GenEvent. There’s a third process, PacketHandler, that has a :push_packet event which forwards the packet to ScoreServer and AnswerServer, as below:

def handle_event({:push_packet, packet}, %{scores: scores, answers: answers} = pids)  do
  ScoreServer.update(scores, packet)
  AnswerServer.update(answers, packet)
  {:ok, pids}
end

This works pretty well, but I’m interested in how it could be reimplemented using GenStage. Am I right in thinking that PacketHandler would be the producer, and ScoreServer and AnswerServer consumers? If so, how would PacketHandler work? Currently the event is triggered by a function call, but the examples I’ve seen of GenStage seem more like a stream. And how does it fit in with the concept of demand, if its dependent on the user choosing an answer?

I’m a bit confused, so any help would be appreciated xD

Most Liked

josevalim

josevalim

Creator of Elixir

Correct. To add to your reply, the PacketHandler likely wants to use the broadcast dispatcher, to ensure every event is broadcast to all consumers.

The GenStage documentation contains two examples of how a PacketHandler could be implemented (see the Broadcaster examples). There is another example in the source code.

wfgilman

wfgilman

Based on my understanding, PacketHandler would be a producer and ScoreServer and AnswerServer would be consumers, as you expected. When they are implemented, they are simply up and running, waiting for information to process. To get things started, when someone answered a question, you would send a message to PacketHandler letting it know there is information to process. Add something like this to the code that creates the %AnswerPacket{}.

send PacketHandler, :new_answer

and something like this to PacketHandler

handle_info(:new_answer, state) do
  ...
 {:noreply, events, state}
end

In the init function of PacketHandler, you can specify the :dispatcher option which allows you to tailor how PacketHandler communicates with ScoreServer and AnswerServer.

Watching this talk a couple of times helped me wrap my head around GenStage.

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics 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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
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
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement