# `Teac.WssClient.Handler`
[🔗](https://codeberg.org/FullStacking/teac)

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)

# `handle_event`

```elixir
@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`

```elixir
@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.).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
