【问题标题】:How to set and retrieve presence status (online/busy/offline) using phoenix presence如何使用 phoenix Presence 设置和检索在线状态(在线/忙碌/离线)
【发布时间】:2017-08-01 06:29:03
【问题描述】:

我想获取使用 phoenix 框架编写的小型聊天应用程序的用户状态。
状态:在线/离线/忙碌/离开

这是注释代码段:

defmodule PhoenixChat.Presence do
  @moduledoc """
  Provides presence tracking to channels and processes.

  See the [`Phoenix.Presence`](http://hexdocs.pm/phoenix/Phoenix.Presence.html)
  docs for more details.

  ## Usage

  Presences can be tracked in your channel after joining:

      defmodule PhoenixChat.MyChannel do
        use PhoenixChat.Web, :channel
        alias PhoenixChat.Presence

        def join("some:topic", _params, socket) do
          send(self, :after_join)
          {:ok, assign(socket, :user_id, ...)}
        end

        def handle_info(:after_join, socket) do
          {:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
            online_at: inspect(System.system_time(:seconds))
          })
          push socket, "presence_state", Presence.list(socket)
          {:noreply, socket}
        end
      end

  In the example above, `Presence.track` is used to register this
  channel's process as a presence for the socket's user ID, with
  a map of metadata. Next, the current presence list for
  the socket's topic is pushed to the client as a `"presence_state"` event.

  Finally, a diff of presence join and leave events will be sent to the
  client as they happen in real-time with the "presence_diff" event.
  See `Phoenix.Presence.list/2` for details on the presence datastructure.

  ## Fetching Presence Information

  The `fetch/2` callback is triggered when using `list/1`
  and serves as a mechanism to fetch presence information a single time,
  before broadcasting the information to all channel subscribers.
  This prevents N query problems and gives you a single place to group
  isolated data fetching to extend presence metadata.

  The function receives a topic and map of presences and must return a
  map of data matching the Presence datastructure:

      %{"123" => %{metas: [%{status: "away", phx_ref: ...}],
        "456" => %{metas: [%{status: "online", phx_ref: ...}]}

  The `:metas` key must be kept, but you can extend the map of information
  to include any additional information. For example:

      def fetch(_topic, entries) do
        query =
          from u in User,
            where: u.id in ^Map.keys(entries),
            select: {u.id, u}

        users = query |> Repo.all |> Enum.into(%{})

        for {key, %{metas: metas}} <- entries, into: %{} do
          {key, %{metas: metas, user: users[key]}}
        end
      end

  The function above fetches all users from the database who
  have registered presences for the given topic. The fetched
  information is then extended with a `:user` key of the user's
  information, while maintaining the required `:metas` field from the
  original presence data.
  """
  use Phoenix.Presence, otp_app: :phoenix_chat,
  pubsub_server: PhoenixChat.PubSub
end

我正在使用此方法获取用户状态PhoenixChat.RoomChannel

def handle_info(:after_join, socket) do
  Presence.track(socket, socket.assigns.user, %{
    online_at: :os.system_time(:millisecond)
  })
  push socket, "presence_state", Presence.list(socket)
    {:noreply, socket}
end

在这个部分 保存用户在线状态

def connect(%{"user" => user, "status" => status}, socket) do
  Redix.command(:redix, ["SET", user, status]) 
  {:ok, assign(socket, :user, user)}
end

我想获得状态而不是时间。 我该如何完成这项任务?

【问题讨论】:

    标签: elixir phoenix-framework


    【解决方案1】:

    您将带有online_at 作为第三个参数的映射传递给Presence.track/3。您可以将任何其他字段放在那里以跟踪状态。请注意,当您想更改状态时,您必须再次调用Presence.track/3

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 2015-05-13
      • 1970-01-01
      • 2013-05-07
      • 1970-01-01
      相关资源
      最近更新 更多