Introducing Event Server
So very first behavior server of OTP that we are gonna look in is Generic(I am guessing what gen means in erlang) event server.
Okay, Jack! first, we look at, as to how an event server should look like. Event server is able to
1.) receive certain event they are bind to,
2.) notify listener bound/subscribed to those events/listeners (or client) should consume those events as and when they happen.
That’s it! To understand this behavior we will build a broadcast event server.
defmodule BroadcastEventHandler do
use GenEvent.Behaviour
def init(_) do
{ :ok, [] }
end
def handle_event({:broadcast, x}, broadcasts) do
{ :ok, [ x | broadcasts ] }
end
def handle_call(:broadcasts, broadcasts) do
{:ok, broadcasts, []}
end
end
This is it. We defined our initial version of the broadcast server. It has two methods apart from initializer. One to receive a published event, another one to list all events that happened since the last conversation with a subscriber.
Jack is dying to use it. Let’s look at how to use it
1.) We need to initiate a gen_event
server. This will return a tuple of {:ok, pid}
as a response
#initiate a gen_event server
{:ok, pid} = :gen_event.start_link
Since we can not directly call or initiate our broadcast handler, we need to register it with the event server
#add hadler to it
:gen_event.add_handler(pid, BroadcastEventHandler, [])
Now our broadcast handler is up and running and ready to receive broadcast messages
:gen_event.notify(pid, {:broadcast, "I dare you!"})
:gen_event.notify(pid, {:broadcast, "Meetup at 10 o'clock"})
So we published two messages to broadcast handler, lets see if it serves them well.
:gen_event.call(pid, BroadcastEventHandler, :broadcasts)
#=> ["Meetup at 10 o'clock", "I dare you!"]
:gen_event.call(pid, BroadcastEventHandler, :broadcasts)
#=> []
Well done boy! It only served them once, when we called again, it returned empty array, as messages were already consumed. Lets do one more round.
:gen_event.notify(pid, {:broadcast, "Assemble in class"})
:gen_event.call(pid, BroadcastEventHandler, :broadcasts)
#=> ["Assemble in class"]
Great going, Jack!