Pushest v0.2.0 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 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:
# 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
use Pushest, otp_app: :simple_client
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
# Now you can start your application with Pushest 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:
SimpleClient.trigger("private-channel", "event", %{message: "via api"})
SimpleClient.channels()
# => %{"channels" => %{"public-channel" => %{}}}
SimpleClient.subscribe("private-channel")
SimpleClient.trigger("private-channel", "event", %{message: "via ws"})
SimpleClient.trigger("private-channel", "event", %{message: "via api"}, force_api: true)
# ...
Link to this section Summary
Callbacks
Invoked when the Pusher event occurs (e.g. other client sends a message)
Link to this section Callbacks
Invoked when the Pusher event occurs (e.g. other client sends a message).