Skip to content

Instantly share code, notes, and snippets.

@TattdCodeMonkey
Last active February 19, 2016 11:51
Show Gist options
  • Save TattdCodeMonkey/60275b1e2ccc4ed45f7a to your computer and use it in GitHub Desktop.
Save TattdCodeMonkey/60275b1e2ccc4ed45f7a to your computer and use it in GitHub Desktop.
simple elixir gproc pub/sub example for blog post
# 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