Skip to content

Instantly share code, notes, and snippets.

@wess
Created May 16, 2017 12:57
Show Gist options
  • Save wess/28e9397bd4bfc1918719574c6b06c20a to your computer and use it in GitHub Desktop.
Save wess/28e9397bd4bfc1918719574c6b06c20a to your computer and use it in GitHub Desktop.
A simple pubsub
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