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