Teac.WssClient.Handler behaviour (teac v1.0.0)

Copy Markdown View Source

Behaviour for handling Twitch EventSub WebSocket events.

Example

defmodule MyApp.TwitchHandler do
  @behaviour Teac.WssClient.Handler

  @impl true
  def handle_event(%{"type" => "channel.follow"} = _subscription, event, state) do
    IO.inspect(event, label: "new follower")
    {:ok, state}
  end

  def handle_event(_subscription, _event, state), do: {:ok, state}

  @impl true
  def handle_revocation(%{"type" => type, "status" => reason} = _subscription, state) do
    IO.puts("Subscription #{type} revoked: #{reason}")
    state
  end
end

Then start the client:

Teac.WssClient.start_link(client, handler: MyApp.TwitchHandler)

Summary

Callbacks

Called when a notification arrives for a subscribed event.

Called when Twitch revokes a subscription.

Callbacks

handle_event(subscription, event, state)

@callback handle_event(
  subscription :: map(),
  event :: map(),
  state :: term()
) :: {:ok, term()} | {:error, term()}

Called when a notification arrives for a subscribed event.

subscription is the full payload.subscription object from the Twitch frame, containing "id", "type", "version", "cost", "condition", and "created_at".

event is the payload.event object specific to the subscription type.

Return {:ok, new_state} to update handler state, or {:error, reason} to log the error and keep the previous state.

handle_revocation(subscription, state)

@callback handle_revocation(
  subscription :: map(),
  state :: term()
) :: term()

Called when Twitch revokes a subscription.

subscription is the full payload.subscription object, containing "id", "type", "version", "status" (the revocation reason), and "condition".

The return value is ignored — implement for side effects (logging, alerting, etc.).