deerob4

deerob4

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

Showing Posts 2 to 1

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
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
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews