View Source Multiverses.Tracker (multiverses_pubsub v0.6.0)

using-multiverses-with-phoenix-tracker

Using Multiverses With Phoenix Tracker

Now set up your configuration:

in-config-exs

in config.exs

config :my_app, Phoenix.PubSub, Phoenix.PubSub
config :my_app, Phoenix.Tracker, Phoenix.Tracker

in-test-exs

in test.exs

config :my_app, Phoenix.PubSub, Multiverse.PubSub
config :my_app, Phoenix.Tracker, Multiverse.Tracker

create-your-tracker-module

Create your tracker module

As usual, you must implement a a Tracker module. This is an example Tracker module that is a direct reinterpretation of the example Phoenix.Tracker module. If you have your own tracker logic, use that instead.

Note that the handle_diff callback will be executed in separate batches, one for each known universe shard.

defmodule MyApp.Tracker do
  use Phoenix.Tracker

  @phoenix_tracker Application.get_env(:my_app, Phoenix.Tracker)
  @phoenix_pubsub Application.get_env(:my_app, Phoenix.PubSub)

  def start_link(opts) do
    opts = Keyword.merge([name: __MODULE__], opts)
    @phoenix_tracker.start_link(__MODULE__, opts, opts)
  end

  def init(opts) do
    server = Keyword.fetch!(opts, :pubsub_server)
    {:ok, %{pubsub_server: server, node_name: Phoenix.PubSub.node_name(server)}}
  end

  def handle_diff(diff, state) do
    for {topic, {joins, leaves}} <- diff do
      for {_key, meta} <- joins do
        @phoenix_pubsub.broadcast(...)
      end

      for {_key, meta} <- leaves do
        @phoenix_pubsub.broadcast(...)
      end
    end

    {:ok, state}
  end
end

wherever-you-use-tracker

wherever you use Tracker

defmodule MyApp.UsesTracker do
  # in place of `alias Phoenix.Tracker`
  @tracker Application.compile_env!(:my_app, Phoenix.Tracker)

  # ... code with @tracker in place of Tracker
end

Link to this section Summary

Link to this section Functions