Created
January 10, 2018 14:27
-
-
Save ospaarmann/07fd5b5c59ec69c00ae7ee6ed435d884 to your computer and use it in GitHub Desktop.
EmojiMap.TweetConsumer
This file contains hidden or 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 EmojiMap.TweetConsumer do | |
@moduledoc """ | |
The consumer side. Subscribes to the producer once it is started so when | |
it crashes and the supervisor restarts it, it automatically re-subscribes | |
""" | |
use GenStage | |
@doc """ | |
Starts the consumer. | |
""" | |
def start_link() do | |
GenStage.start_link(__MODULE__, :ok) | |
end | |
def init(:ok) do | |
# Starts a permanent subscription to the broadcaster | |
# which will automatically start requesting items. | |
{:consumer, :ok, subscribe_to: [EmojiMap.TweetBroadcaster]} | |
end | |
def handle_events(events, _from, state) do | |
for event <- events do | |
# Broadcast it via our Websocket | |
EmojiMap.Endpoint.broadcast "map:updates", "new:msg", %{attributes: event} | |
end | |
{:noreply, [], state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment