Skip to content

Instantly share code, notes, and snippets.

@rlivsey
Last active July 22, 2016 10:30
Show Gist options
  • Save rlivsey/79f4541c2677601ef01c91d4bcef68ca to your computer and use it in GitHub Desktop.
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
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