Twitter 如何从spawn函数创建GenServer子进程?

Twitter 如何从spawn函数创建GenServer子进程?,twitter,elixir,phoenix-framework,gen-server,phoenix-channels,Twitter,Elixir,Phoenix Framework,Gen Server,Phoenix Channels,我用它来实时跟踪推文,并通过频道端点进行广播。我想为每个事件创建一个进程,并为其分配一个twitter流侦听器,然后当新订阅者加入到同一事件时,将获得以前的流状态,并接收和广播新的tweet 如何从以下位置创建GenServer进程: stream = ExTwitter.stream_filter(track: hashtags) pid = spawn(fn -> for tweet <- stream do IO.puts tweet.text MyApp.

我用它来实时跟踪推文,并通过频道端点进行广播。我想为每个事件创建一个进程,并为其分配一个twitter流侦听器,然后当新订阅者加入到同一事件时,将获得以前的流状态,并接收和广播新的tweet

如何从以下位置创建GenServer进程:

stream = ExTwitter.stream_filter(track: hashtags)
pid = spawn(fn ->
  for tweet <- stream do
    IO.puts tweet.text
    MyApp.Endpoint.broadcast! "stream", "tweet", %{tweet: tweet.text}
  end
end)
defmodule MyApp.TwitterStream.Monitor do
  require IEx
  @moduledoc """
  Store twitter stream per event_id
  """
  use GenServer

  def create(event_id, hashtags, coords) do
    case GenServer.whereis(ref(event_id)) do
      nil ->
        Supervisor.start_child(MyApp.TwitterStream.Supervisor, [event_id, hashtags, coords])
      _twitter_stream ->
        IEx.pry
        # return previous ExTwitter stream state and broadcast new tweets
        {:error, :twitter_stream_already_exists}
    end
  end

  def start_link(event_id, hashtags, coords) do
    # stream = ExTwitter.stream_filter(track: hashtags)
    # pid = spawn(fn ->
    #   for tweet <- stream do
    #     IO.puts tweet.text
    #     MyApp.Endpoint.broadcast! "stream", "tweet", %{tweet: tweet.text}
    #   end
    # end)
    GenServer.start_link(__MODULE__, %{hashtags: hashtags, coords: coords}, name: ref(event_id))
  end

  def stop(event_id) do
    try_call(event_id, :stop)
  end

  def info(event_id) do
    try_call(event_id, :info)
  end

  def handle_call(:stop, _from, state) do
    # ExTwitter.stream_control(pid, :stop)
    {:stop, :normal, :ok, state}
  end

  def handle_call(:info, _from, state) do
    {:reply, state, state}
  end

  defp try_call(event_id, call_function) do
    case GenServer.whereis(ref(event_id)) do
      nil ->
        {:error, :invalid_twitter_stream}
      twitter_stream ->
        GenServer.call(twitter_stream, call_function)
    end
  end

  defp ref(event_id) do
    {:global, {:twitter_stream, event_id}}
  end
end
defmodule MyApp.TwitterStream.Supervisor do
  use Supervisor

  def start_link do
    Supervisor.start_link(__MODULE__, :ok, name: __MODULE__)
  end

  def init(:ok) do
    children = [
      worker(MyApp.TwitterStream.Monitor, [], restart: :temporary)
    ]

    supervise(children, strategy: :simple_one_for_one)
  end
end