Last active
February 19, 2016 11:51
-
-
Save TattdCodeMonkey/60275b1e2ccc4ed45f7a to your computer and use it in GitHub Desktop.
simple elixir gproc pub/sub example for blog post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# supervisor.ex | |
defmodule EventSup do | |
use Supervisor | |
def start_link, do: Supervisor.start_link(__MODULE__, [], []) | |
def init(_) do | |
supervise( | |
[ | |
worker(EventHandler, [:event_manager]) | |
], | |
[strategy: :one_for_one] | |
) | |
end | |
end | |
# event_handler.ex | |
defmodule EventHandler do | |
use GenServer | |
require Logger | |
def start_link(topic), do: GenServer.start_link(__MODULE__, topic, []) | |
def init(topic) do | |
:gproc.reg({:p, :l, topic}) | |
{:ok, topic} | |
end | |
def handle_info(msg, topic) do | |
Logger.info "received message #{inspect msg}" | |
{:noreply, topic} | |
end | |
end | |
# send message | |
iex>:gproc.send({:p, :l, :event_manager}, {:message, "stuff"}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment