Created
June 4, 2016 22:33
-
-
Save nicholasjhenry/a0af33b618773cab391fcd3aefcf8b00 to your computer and use it in GitHub Desktop.
Event Notification in Phoenix with PubSub
This file contains hidden or 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 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