Skip to content

Instantly share code, notes, and snippets.

@narrowtux
Created October 24, 2016 11:10
Show Gist options
  • Save narrowtux/c5160c62a7e10e7f2da5c4fd97ccce0f to your computer and use it in GitHub Desktop.
Save narrowtux/c5160c62a7e10e7f2da5c4fd97ccce0f to your computer and use it in GitHub Desktop.
Phoenix.Channel + GenStage
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
@narrowtux
Copy link
Author

I only tried this for consumers. Producer stages need much more work since handle_in, etc. don't support the {reply, events, state} return.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment