Skip to content

v0.8.3 - Subscribe For Your Presence Now

Choose a tag to compare
@grych grych released this 22 Jun 18:05
· 113 commits to master since this release

This version brings two useful features: presence and ability to subscribe to topics in the runtime.

Upgrading from =< 0.8.2

Please ensure you have set :main_phoenix_app in your config.exs. The way how Drab is searching
for the Phoenix app it is working on, has been changed.

Subscribe and unsubscribe from external topics in a runtime

Finally, you are not limited to the compile-time topic you’ve set with broadcasting/1 macro in the
commander. Now you can subscribe/2 to the external topic, receiving broadcasts sent to it.

subscribe(socket, same_action(MyApp.MyController, :index))
subscribe(socket, same_topic("user_#{user_id}"))


Conveniences for Phoenix.Presence
If configured (it is disabled by default), tracks the user presence on the topic. The following
example shows the number of connected users, live:

defmodule MyAppWeb.MyCommander
  use Drab.Commander

  broadcasting "global"
  onconnect :connected
  ondisconnect :disconnected

  def connected(socket) do
    broadcast_html socket, "#number_of_users", Drab.Presence.count_users(socket)

  def disconnected(_store, _session) do
    topic = same_topic("global")
    broadcast_html topic, "#number_of_users", Drab.Presence.count_users(topic)

By default, presence map key is set as a browser UUID (which is shamelessly stored in the local
store in the browser), but it may be also any session value. This may be useful, if you have the
user_id already in the session, just configure it:

config :drab, :presence, id: :user_id

Updated enable/disable when processing behaviour

After launching an event from the page, the control (button) is disable until processing stops.
Now it is even better, as it recognizes previously disabled controls (#146).