Hologram.Realtime (hologram v0.9.1)

Copy Markdown View Source

Public API for Hologram's realtime layer.

Summary

Functions

Broadcasts an action to subscribers of the given channel.

Broadcasts an action to subscribers of the given channel, excluding the listed identities.

Subscribes the connections identified by identity to a {channel, cid} binding.

Unsubscribes a {channel, cid} binding under identity.

Unsubscribes every cid binding under identity on channel.

Functions

broadcast_action(channel, action_name, params \\ %{})

@spec broadcast_action(atom() | tuple(), atom(), keyword() | map()) :: :ok

Broadcasts an action to subscribers of the given channel.

Immediate counterpart to put_broadcast. Called from outside Hologram.Page and Hologram.Component handlers (init/3, command/3) - e.g. background jobs, GenServers, controllers, plugs, mix tasks. Silent no-op if no connections are subscribed. Delivered to every cid that registered the channel via put_subscription on each receiving connection.

broadcast_action_except(excluded, channel, action_name, params \\ %{})

@spec broadcast_action_except(
  tuple() | [tuple()],
  atom() | tuple(),
  atom(),
  keyword() | map()
) :: :ok

Broadcasts an action to subscribers of the given channel, excluding the listed identities.

Like broadcast_action/3 but takes an excluded_identities argument first. Pass either a single identity tuple - {:instance, id}, {:session, id}, or {:user, id} - or a list of such tuples. A connection matching any listed identity does not receive the broadcast.

subscribe(identity, channel, cid)

@spec subscribe(
  {:instance, String.t()} | {:session, term()} | {:user, term()},
  atom() | tuple(),
  String.t()
) :: :ok

Subscribes the connections identified by identity to a {channel, cid} binding.

identity is {:instance, id}, {:session, id}, or {:user, id}. Every live connection matching the identity begins receiving broadcasts on channel for the given cid. An explicit subscribe is a re-grant that supersedes any prior unsubscribe/unsubscribe_all for the same identity and channel.

When identity resolves to no live connection, the call has no effect on delivery. Returns :ok. Raises ArgumentError on an invalid channel.

unsubscribe(identity, channel, cid)

@spec unsubscribe(
  {:instance, String.t()} | {:session, term()} | {:user, term()},
  atom() | tuple(),
  String.t()
) :: :ok

Unsubscribes a {channel, cid} binding under identity.

Raises ArgumentError on an invalid channel.

unsubscribe_all(identity, channel)

@spec unsubscribe_all(
  {:instance, String.t()} | {:session, term()} | {:user, term()},
  atom() | tuple()
) :: :ok

Unsubscribes every cid binding under identity on channel.

Raises ArgumentError on an invalid channel.