Skip to content

Instantly share code, notes, and snippets.

@alexgaribay
Last active January 19, 2016 20:04
Show Gist options
  • Save alexgaribay/36836094838665e2008f to your computer and use it in GitHub Desktop.
Save alexgaribay/36836094838665e2008f to your computer and use it in GitHub Desktop.
defmodule App.Worker do
def do_work(topic, user) do
# async work goes here
App.Endpoint.broadcast! topic, "work_done", %{ "user" => user }
end
end
defmodule App.Channel do
use App.Web, :channel
# Intercept broadcast messages you're interested in
intercepts ["work_done"]
# boilerplate code here
# Do some work
def handle_in("do_work", _params, socket) do
App.Worker.do_work(socket.topic, socket.assigns[:user])
{ :reply, :ok, socket }
end
# Handle the intercept
def handle_out("work_done", params, socket) do
if socket.assigns[:user] == params["user"] do
push socket, "work_done", %{}
end
{ :noreply, socket }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment