# useActiveTurns Subscribe to the set of active turns on the channel. Returns a reactive `Map` keyed by `clientId`, where each value is a `Set` of `turnId`s for that client. Re-renders when turns start or end. Use it to drive UI such as a send-or-stop button, per-client streaming indicators, or a "wait for all turns to finish" guard. This hook must be used within a [`TransportProvider`](https://ably.com/docs/ai-transport/api/react/providers.md#TransportProvider) or a [`ChatTransportProvider`](https://ably.com/docs/ai-transport/api/react/providers.md#ChatTransportProvider). #### React ``` import { useActiveTurns } from '@ably/ai-transport/react'; function SendOrStop({ onSend, onStop }) { const activeTurns = useActiveTurns(); return activeTurns.size > 0 ? : ; } ``` ## Parameters | Prop | Required | Description | Type | | --- | --- | --- | --- | | transport | Optional | Transport to track turns for. Defaults to the nearest provider when omitted. | `ClientTransport` or Null |
## Returns `Map>` A reactive map. The outer key is the `clientId` that owns the turns. The inner `Set` holds the `turnId`s currently active for that client. Common patterns: - `activeTurns.size > 0` — any turn is active anywhere on the channel. - `activeTurns.has('agent-1')` — a specific client (typically the agent) has at least one active turn. - `activeTurns.get(clientId)?.size ?? 0` — how many turns a specific client has open. The map updates in real time across every connected client, not just the local one. If another tab on the same session starts a turn, this hook's value updates in this component too. ## Example ### React ``` import { useActiveTurns, useView } from '@ably/ai-transport/react'; import { useState } from 'react'; function Composer() { const { send } = useView(); const activeTurns = useActiveTurns(); const [input, setInput] = useState(''); const isStreaming = activeTurns.size > 0; const onSend = async () => { const text = input; setInput(''); await send({ id: crypto.randomUUID(), role: 'user', parts: [{ type: 'text', text }] }); }; return (
{ e.preventDefault(); onSend(); }}> setInput(e.target.value)} /> {isStreaming ? : }
); } ```
## Related Topics - [Providers](https://ably.com/docs/ai-transport/api/react/providers.md): TransportProvider and ChatTransportProvider for the AI Transport React integration. - [useClientTransport](https://ably.com/docs/ai-transport/api/react/use-client-transport.md): Read a ClientTransport from the nearest TransportProvider in the AI Transport React integration. - [useView](https://ably.com/docs/ai-transport/api/react/use-view.md): Subscribe to a paginated, branch-aware view of the AI Transport conversation tree from React. - [useCreateView](https://ably.com/docs/ai-transport/api/react/use-create-view.md): Create an independent View over the AI Transport conversation tree from React, with its own branch selections and pagination. - [useTree](https://ably.com/docs/ai-transport/api/react/use-tree.md): Stable structural query callbacks for the AI Transport conversation tree from React. - [useAblyMessages](https://ably.com/docs/ai-transport/api/react/use-ably-messages.md): Subscribe to raw Ably InboundMessages on the AI Transport channel from React. - [useChatTransport](https://ably.com/docs/ai-transport/api/react/use-chat-transport.md): Read a ChatTransport and its underlying ClientTransport from the nearest ChatTransportProvider in AI Transport. - [useMessageSync](https://ably.com/docs/ai-transport/api/react/use-message-sync.md): Wire AI Transport message updates into Vercel useChat's message state from React. ## Documentation Index To discover additional Ably documentation: 1. Fetch [llms.txt](https://ably.com/llms.txt) for the canonical list of available pages. 2. Identify relevant URLs from that index. 3. Fetch target pages as needed. Avoid using assumed or outdated documentation paths.