Created
May 16, 2017 12:57
-
-
Save wess/28e9397bd4bfc1918719574c6b06c20a to your computer and use it in GitHub Desktop.
A simple pubsub
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
defmodule Howl do | |
use GenServer | |
require Logger | |
@type type :: :n | :p | :c | :a | :r | :rc | |
@type scope :: :l | :g | |
@type name :: term | |
@type key :: {type, scope, name} | |
# Client API | |
def subscribe(topic) do | |
:gproc.reg({:p,:l,topic}) | |
end | |
def subscribers(topic) do | |
:gproc.lookup_pids({:p, :l, topic}) | |
end | |
def broadcast(topic, message) do | |
:gproc.send({:p, :l, topic}, message) | |
end | |
# Server API | |
def init(args) do | |
:gproc.reg({:p, :l, args[:topic]}) | |
{:ok, []} | |
end | |
def handle_info(message, state) do | |
IO.inspect "Got #{inspect message} in process #{inspect self()}" | |
{:noreply, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment