Last active
January 19, 2016 20:04
-
-
Save alexgaribay/36836094838665e2008f to your computer and use it in GitHub Desktop.
This file contains 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 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