Last active
July 22, 2016 10:30
-
-
Save rlivsey/79f4541c2677601ef01c91d4bcef68ca to your computer and use it in GitHub Desktop.
Subscribing to events from a Phoenix.Channel in 1.2.0-rc.0
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.TestChannel do | |
use MyApp.Web, :channel | |
require Logger | |
intercept ["some-event"] | |
def join("test:lobby", _, socket) do | |
# subscribe to a topic | |
subscribe socket, "some-topic" | |
{:ok, socket} | |
end | |
def handle_out("some-event", payload, socket) do | |
Logger.info("some-topic:some-event called with #{inspect payload}") | |
{:noreply, socket} | |
end | |
end | |
# now anywhere else in the app we can broadcast an event which the channel can handle | |
MyApp.Endpoint.broadcast "some-topic", "some-event", %{ foo: "bar" } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment