Created
October 24, 2016 11:10
-
-
Save narrowtux/c5160c62a7e10e7f2da5c4fd97ccce0f to your computer and use it in GitHub Desktop.
Phoenix.Channel + GenStage
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 MyApp.Channel do | |
alias Experimental.GenStage | |
use GenStage | |
use MyApp.Web, :channel | |
def init(socket) do | |
{:consumer, socket, subscribe_to: [MyApp.BroadcastStage]} | |
end | |
def handle_events(events, _from, socket) do | |
# get events pushed directly to each channel connection | |
Enum.each events, &Phoenix.Channel.push(socket, "message", &1) | |
{:noreply, [], socket} | |
end | |
def join(topic, params, socket) do | |
# call start_link when you want to accept the connection | |
GenStage.start_link(__MODULE__, socket) | |
{:ok, socket} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I only tried this for consumers. Producer stages need much more work since
handle_in
, etc. don't support the{reply, events, state}
return.