Pushest v0.1.4 Pushest behaviour View Source

Pushest handles communication with Pusher server via wesockets. Abstracts un/subscription, client-side triggers, private/presence channel authorizations. Keeps track of subscribed channels and users presence when subscribed to presence channel. Pushest is meant to be used in your module where you can define callbacks for events you’re interested in.

A simple implementation would be:

defmodule SimpleClient do
  use Pushest

  def start_link() do
    options = %{
      cluster: "eu",
      encrypted: true,
      secret: "SECRET"
    }
    Pushest.start_link("APP_KEY", options, __MODULE__, name: __MODULE__)
  end

  def handle_event({:ok, "public-channel", "some-event"}, frame) do
    # do something with public frame
  end

  def handle_event({:ok, "private-channel", "some-other-event"}, frame) do
    # do something with private frame
  end
end

Link to this section Summary

Types

Options for Pushest to properly communicate with Pusher server

Functions

Returns a specification to start this module under a supervisor

Sync server-side callback returning current presence information. Contains IDs of all the subscribed users and optional informations about them

Async server-side callback handling event triggers with a data payload. Sends WS frame as a sideeffect

Handles varios Pusher events, updates state and tries to call user-defined callbacks

Starts the @client connection to the Pusher URL and upgrades it to WS/S communication. Stores @client.conn PID in the state

Starts a Pushest process linked to current process. Please note, you need to provide a module as a third element, Pushest will try to invoke handle_event callbacks in that module when Pusher event occurs

Callbacks

Invoked when the Pusher event occurs (e.g. other client sends a message)

Link to this section Types

Link to this type pusher_opts() View Source
pusher_opts() :: %{
  cluster: String.t(),
  encrypted: boolean(),
  secret: String.t()
}

Options for Pushest to properly communicate with Pusher server.

  • :cluster - Cluster where your Pusher app is configured.
  • :encrypted - When set to true communication with Pusher is fully encrypted.
  • :secret - Necessary to subscribe to private/presence channels and trigger events.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function handle_call(msg, from, state) View Source
handle_call(:channels | :presence, {pid(), term()}, %Pushest.Data.State{
  app_key: term(),
  channels: term(),
  conn_pid: term(),
  module: term(),
  options: term(),
  presence: term(),
  socket_info: term(),
  url: term()
}) ::
  {:reply,
   list()
   | %Pushest.Data.Presence{
       count: term(),
       hash: term(),
       ids: term(),
       me: term()
     },
   %Pushest.Data.State{
     app_key: term(),
     channels: term(),
     conn_pid: term(),
     module: term(),
     options: term(),
     presence: term(),
     socket_info: term(),
     url: term()
   }}

Sync server-side callback returning current presence information. Contains IDs of all the subscribed users and optional informations about them.

Link to this function handle_cast(msg, state) View Source
handle_cast({atom(), String.t(), map()}, %Pushest.Data.State{
  app_key: term(),
  channels: term(),
  conn_pid: term(),
  module: term(),
  options: term(),
  presence: term(),
  socket_info: term(),
  url: term()
}) ::
  {:noreply,
   %Pushest.Data.State{
     app_key: term(),
     channels: term(),
     conn_pid: term(),
     module: term(),
     options: term(),
     presence: term(),
     socket_info: term(),
     url: term()
   }}

Async server-side callback handling event triggers with a data payload. Sends WS frame as a sideeffect.

Link to this function handle_info(msg, state) View Source
handle_info(term(), %Pushest.Data.State{
  app_key: term(),
  channels: term(),
  conn_pid: term(),
  module: term(),
  options: term(),
  presence: term(),
  socket_info: term(),
  url: term()
}) ::
  {:noreply,
   %Pushest.Data.State{
     app_key: term(),
     channels: term(),
     conn_pid: term(),
     module: term(),
     options: term(),
     presence: term(),
     socket_info: term(),
     url: term()
   }}

Handles varios Pusher events, updates state and tries to call user-defined callbacks.

Link to this function init(state) View Source
init(%Pushest.Data.State{
  app_key: term(),
  channels: term(),
  conn_pid: term(),
  module: term(),
  options: term(),
  presence: term(),
  socket_info: term(),
  url: term()
}) ::
  {:ok,
   %Pushest.Data.State{
     app_key: term(),
     channels: term(),
     conn_pid: term(),
     module: term(),
     options: term(),
     presence: term(),
     socket_info: term(),
     url: term()
   }}

Starts the @client connection to the Pusher URL and upgrades it to WS/S communication. Stores @client.conn PID in the state.

Link to this function start_link(app_key, pusher_opts, module, opts \\ []) View Source
start_link(String.t(), pusher_opts(), module(), list()) ::
  {:ok, pid()} | {:error, term()}

Starts a Pushest process linked to current process. Please note, you need to provide a module as a third element, Pushest will try to invoke handle_event callbacks in that module when Pusher event occurs.

For available pusher_opts values see pusher_opts/0.

Link to this section Callbacks

Link to this callback handle_event({}, term) View Source
handle_event({atom(), String.t(), String.t()}, term()) :: term()

Invoked when the Pusher event occurs (e.g. other client sends a message).