Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created June 4, 2016 22:33
Show Gist options
  • Select an option

  • Save nicholasjhenry/a0af33b618773cab391fcd3aefcf8b00 to your computer and use it in GitHub Desktop.

Select an option

Save nicholasjhenry/a0af33b618773cab391fcd3aefcf8b00 to your computer and use it in GitHub Desktop.
Event Notification in Phoenix with PubSub
defmodule MyApp.Subscriber do
use GenServer
def start_link(channel) do
GenServer.start_link(__MODULE__, channel)
end
def init(channel) do
pid = self
ref = MyApp.Endpoint.subscribe(pid, channel)
{:ok, {pid, channel, ref}}
end
def handle_info(%{event: "test"} = message, state) do
IO.inspect "#######################"
IO.inspect "Test - Received Message:"
IO.inspect message
IO.inspect "#######################"
{:noreply, state}
end
def handle_info(message, state) do
IO.inspect "#######################"
IO.inspect "Catch All - Received Message:"
IO.inspect message
IO.inspect "#######################"
{:noreply, state}
end
end
# MyApp module
worker(MusicAccent.Subscriber, ["events:all"])
# IEx
MyApp.Endpoint.broadcast!("events:all", "test", %{foo: "bar"})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment