Pushest v0.2.2 Pushest behaviour View Source

Pushest is a Pusher library leveraging Elixir/OTP to combine server and client-side Pusher features. Abstracts un/subscription, client-side triggers, private/presence channel authorizations. Keeps track of subscribed channels and users presence when subscribed to a 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 in an OTP application would be:

# Add necessary pusher configuration to your application config (assuming an OTP app):
# simple_client/config/config.exs
config :simple_client, SimpleClient,
  pusher_app_id: System.get_env("PUSHER_APP_ID"),
  pusher_key: System.get_env("PUSHER_APP_KEY"),
  pusher_secret: System.get_env("PUSHER_SECRET"),
  pusher_cluster: System.get_env("PUSHER_CLUSTER"),
  pusher_encrypted: true

# simple_client/simple_client.ex
defmodule SimpleClient do
  # :otp_app option is needed for Pushest to get a config.
  use Pushest, otp_app: :simple_client

  # Subscribe to these channels right after application startup.
  def init_channels do
    [
      [name: "public-init-channel", user_data: %{}],
      [name: "private-init-channel", user_data: %{}],
      [name: "presence-init-channel", user_data: %{user_id: 123}],
    ]
  end

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

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

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

# Now you can start your application as a part of your supervision tree:
# simple_client/lib/simple_client/application.ex
def start(_type, _args) do
  children = [
    {SimpleClient, []}
  ]

  opts = [strategy: :one_for_one, name: Sup.Supervisor]
  Supervisor.start_link(children, opts)
end

You can also provide Pusher options directly via start_link/1 (without using OTP app configuration):

config = %{
  app_id:  System.get_env("PUSHER_APP_ID"),
  key: System.get_env("PUSHER_APP_KEY"),
  secret: System.get_env("PUSHER_SECRET"),
  cluster: System.get_env("PUSHER_CLUSTER"),
  encrypted: true
}

{:ok, pid} = SimpleClient.start_link(config)

Now you can interact with Pusher using methods injected in your module:

SimpleClient.trigger("private-channel", "event", %{message: "via api"}) 
SimpleClient.channels()
# => %{
"channels" => %{
  "presence-init-channel" => %{},
  "private-init-channel" => %{},
  "public-init-channel" => %{}
}
SimpleClient.subscribe("private-channel")
SimpleClient.trigger("private-channel", "event", %{message: "via ws"}) 
SimpleClient.trigger("private-channel", "event", %{message: "via api"}, force_api: true) 
# ...

For full list of injected methods please check the README.

Link to this section Summary

Callbacks

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

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).