Teac.WssClient (teac v1.0.0)

Copy Markdown View Source

Summary

Functions

Assigns this WebSocket session to a conduit shard.

Returns a child spec for use in a supervision tree.

Starts a WebSocket connection to the Twitch EventSub endpoint.

Gracefully closes the WebSocket connection with a normal close frame.

Creates an EventSub subscription tied to this WebSocket session.

Deletes an EventSub subscription by ID.

Functions

assign_shard(pid, opts)

Assigns this WebSocket session to a conduit shard.

For conduit-based setups, call this instead of subscribe/2 within 10 seconds of session_welcome. Subscriptions are then created separately via Teac.Api.EventSub.Subscriptions.post/2 using the conduit ID as transport.

Options

  • :conduit_id - required. The conduit to assign this session to.
  • :shard_id - required. The shard ID (zero-based string) to assign.

Example

Teac.WssClient.assign_shard(pid, conduit_id: "abc123", shard_id: "0")

child_spec(state)

Returns a child spec for use in a supervision tree.

The argument must be a two-element tuple of {client, opts}, where opts is the same keyword list accepted by start_link/2. A :name opt is recommended so the process can be looked up by name.

Example

children = [
  {Teac.WssClient, {client, handler: MyApp.TwitchHandler, name: MyApp.TwitchSocket}}
]

Supervisor.start_link(children, strategy: :one_for_one)

start_link(client, opts \\ [])

Starts a WebSocket connection to the Twitch EventSub endpoint.

Options

  • :handler - required. A module implementing Teac.WssClient.Handler.
  • :handler_state - optional initial state passed to handler callbacks. Defaults to nil.
  • :keepalive_timeout_seconds - optional integer (10–600). Tells Twitch how often to send keepalive frames. Defaults to Twitch's server-side default (10s).

Example

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

stop(pid)

Gracefully closes the WebSocket connection with a normal close frame.

subscribe(pid, opts)

Creates an EventSub subscription tied to this WebSocket session.

Must be called after the connection is established (i.e. after session_welcome has been received). Twitch requires at least one subscription within 10 seconds of the welcome message or it closes the connection with code 4003.

Options

  • :type - required. The subscription type, e.g. "channel.follow".
  • :version - required. The subscription version, e.g. "2".
  • :condition - required. A map of condition parameters for the subscription type.

Example

Teac.WssClient.subscribe(pid,
  type: "channel.follow",
  version: "2",
  condition: %{"broadcaster_user_id" => "123", "moderator_user_id" => "123"}
)

unsubscribe(pid, subscription_id)

Deletes an EventSub subscription by ID.

Example

Teac.WssClient.unsubscribe(pid, "2d9e9f1b-9b7a-4e6a-8f3c-1a2b3c4d5e6f")