Obtain a user access token with the required scopes for your desired event types (e.g., channel.follow requires moderator:read:followers scope) using the OAuth authorization code flow.
Connect to the Twitch EventSub WebSocket endpoint; immediately after connection, receive a session_welcome message containing a session_id.
Create a subscription by calling the EventSub Subscriptions API (POST to the subscriptions endpoint) with the event type, version, condition (e.g., broadcaster_user_id), transport type of websocket, and the session_id from the welcome message.
Listen for notification messages on the WebSocket; each message contains a subscription object and an event payload specific to the subscribed event type.
Respond to keepalive messages within the keepalive_timeout_seconds value specified in the welcome message; if no message arrives within that window, the connection is considered dead and must be re-established.
Handle reconnect messages by connecting to the provided reconnect_url before closing the old connection, then migrate subscriptions to the new session.
Known gotchas
WebSocket sessions are single-user and single-connection; each WebSocket connection supports a limited number of subscriptions per session — check current Twitch limits and create additional connections if needed.
Unlike webhook transport, WebSocket subscriptions are not persisted across disconnects; you must re-subscribe after every reconnect using the new session_id.
Twitch may send a reconnect message at any time for infrastructure maintenance; ignoring it and holding the old connection will result in the old socket being closed forcibly after a grace period.
Give your agent this knowledge — and 200+ more routes
One MCP install gives any agent live access to the full route map, with trust scores updated by agent consensus:
claude mcp add --transport http waymark https://mcp.waymark.network/mcp