Git Product home page Git Product logo

phoenix_presence_list's Introduction

PhoenixPresenceList

Keep a presence list up to date using broadcasted presence_diff data.

The functionality in this package is closely modelled after what the syncState and syncDiff functions on Presence in phoenix.js provide. Just like you would use syncState and syncDiff in the browser to keep the presence list inside the browser up to date, you can use the supplied PhoenixPresenceList module to keep a presence list inside a BEAM process up to date.

Usage example

Say you have a GenServer process that you would like to subscribe to presence changes on a given topic and have that process keep track of the presence list.

Your GenServer module is named FooWeb.Bar, phoenix presence module is named FooWeb.Presence, your endpoint is named FooWeb.Endpoint and you have a channel with the topic lobby where you are tracking presence.

Then you could do something like this in order to keep a list of presences inside FooWeb.Bar:

defmodule FooWeb.Bar do
  use GenServer
  import PhoenixPresenceList, only: [apply_diff: 2]
  alias Phoenix.Socket.Broadcast

  def start_link(opts) do
    GenServer.start_link(__MODULE__, opts)
  end

  def list(server) do
    GenServer.call(server, :list)
  end

  def init(opts) do
    FooWeb.Endpoint.subscribe("lobby")
    initial_state = FooWeb.Presence.list("lobby")
    {:ok, initial_state}
  end

  def handle_call(:list, _from, state) do
    {:reply, Map.keys(state) |> MapSet.new(), state}
  end

  def handle_info(%Broadcast{event: "presence_diff", payload: payload}, state) do
    {state, _joined, _left} = apply_diff(state, payload)
    {:noreply, state}
  end
end

For a more complete example have a look at this test.

Installation

This package can be installed by adding phoenix_presence_list to your list of dependencies in mix.exs:

def deps do
  [
    {:phoenix_presence_list, "~> 0.1.0"}
  ]
end

License

This project is licensed under the MIT License - see the LICENSE.txt file for details

phoenix_presence_list's People

Contributors

rmoorman avatar

Watchers

 avatar  avatar  avatar

phoenix_presence_list's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.