# Providers
The Ably Chat React SDK provides two context providers that supply chat functionality to your component tree. All chat hooks must be used within these providers.
An API key is required to authenticate with Ably. API keys are used either to authenticate directly with Ably using basic authentication, or to generate tokens for untrusted clients using [JWT authentication](https://ably.com/docs/auth/token.md#jwt). Authenticate an `Ably.Realtime` Pub/Sub client and then pass it to the `ChatClientProvider`.
#### React
```
import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';
import { ChatClientProvider, ChatRoomProvider } from '@ably/chat/react';
const realtimeClient = new Ably.Realtime({ key: 'your-api-key', clientId: 'my-client-id' });
const chatClient = new ChatClient(realtimeClient);
function App() {
return (
{/* Chat hooks can be used here */}
);
}
```
## ChatClientProvider
Provides a `ChatClient` instance to all descendant components via React context. All chat hooks, including [`useChatClient`](https://ably.com/docs/chat/api/react/use-chat-client.md) and [`useChatConnection`](https://ably.com/docs/chat/api/react/use-chat-connection.md), require this provider to be present in the component tree.
The `client` property should be memoized to prevent unnecessary context updates. The provider manages room reference counting internally, only detaching rooms when no references remain.
### Properties
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| client | Required | An instance of the ChatClient. | ChatClient |
| children | Optional | Child components that will have access to the chat client context. | ReactNode or ReactNode[] |
## ChatRoomProvider
Provides room context to descendant components. All room-level hooks such as [`useMessages`](https://ably.com/docs/chat/api/react/use-messages.md), [`usePresence`](https://ably.com/docs/chat/api/react/use-presence.md), and [`useTyping`](https://ably.com/docs/chat/api/react/use-typing.md) require this provider.
The provider automatically handles room attachment and detachment. Multiple providers for the same room with the same options share the same underlying room instance through reference counting.
### Properties
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| name | Required | The name of the room. | String |
| options | Optional | Options to use when creating the room. Must be memoized to prevent unnecessary room recreations. Room options are immutable after creation; using different options for the same room name will reject with a `RoomExistsWithDifferentOptions` error. |
|
| children | Optional | Child components that will have access to the room context. | ReactNode or ReactNode[] |
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| heartbeatThrottleMs | Optional | Minimum time in milliseconds between consecutive typing started events. The first call emits immediately; later calls are no-ops until the interval has elapsed. Calling typing.stop resets the interval. The default value is `10000`. | Number |
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| enableEvents | Optional | Whether the client receives presence events from the server. Can be disabled if presence is used but this client does not need the messages. The default value is `true`. | Boolean |
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| enableEvents | Optional | Whether to receive occupancy events. Enabling this increases message volume as the server sends additional updates for occupancy changes. The default value is `false`. | Boolean |
| Property | Required | Description | Type |
| --- | --- | --- | --- |
| rawMessageReactions | Optional | Whether to receive raw individual message reactions from the realtime channel. Reaction summaries remain available regardless of this setting. The default value is `false`. | Boolean |
| defaultMessageReactionType | Optional | The default message reaction type for sending reactions. Individual types can still be specified via the send method parameter. The default value is `Distinct`. |
|
| Value | Description |
| --- | --- |
| Distinct | Allows at most one reaction of each type per client per message. Duplicates are not counted in the summary. Similar to reactions on Slack. The value is `distinct`. |
| Multiple | Allows any number of reactions, including repeats, counted in the summary. The reaction payload includes a count. Similar to the clap feature on Medium. The value is `multiple`. |
| Unique | Allows at most one reaction per client per message. If a client reacts again, only the second reaction is counted. Similar to reactions on iMessage or WhatsApp. The value is `unique`. |
## ErrorInfo
A standardized, generic Ably error object that contains an Ably-specific status code, and a generic HTTP status code. All errors returned from Ably are compatible with the `ErrorInfo` structure.
| Property | Description | Type |
| --- | --- | --- |
| code | Ably-specific error code. | Number |
| statusCode | HTTP status code corresponding to this error, where applicable. | Number |
| message | Additional information about the error. | String |
| cause | The underlying cause of the error, where applicable. | [ErrorInfo](#errorInfo) or Undefined |
| detail | Optional map of string key-value pairs containing structured metadata associated with the error. | `Record` or Undefined |
## DiscontinuityListener
A callback to detect and respond to discontinuities in the event stream. Discontinuities occur when the client may have missed events due to connectivity issues.
| Parameter | Description | Type |
| --- | --- | --- |
| error | An error providing context about why the discontinuity occurred. | [ErrorInfo](#errorInfo) |
## Example
### React
```
import * as Ably from 'ably';
import { ChatClient } from '@ably/chat';
import { ChatClientProvider, ChatRoomProvider } from '@ably/chat/react';
import { useMemo } from 'react';
// Create clients outside of components
// Use JWT token authentication in production
const realtimeClient = new Ably.Realtime({
key: 'your-api-key',
clientId: 'user-123'
});
const chatClient = new ChatClient(realtimeClient);
function App() {
// Memoize room options to prevent unnecessary recreations
const roomOptions = useMemo(() => ({
typing: { heartbeatThrottleMs: 5000 },
occupancy: { enableEvents: true },
}), []);
return (
);
}
function ChatRoom() {
// All chat hooks can be used here
return
Chat room content
;
}
```
## Related Topics
- [useChatClient](https://ably.com/docs/chat/api/react/use-chat-client.md): API reference for the useChatClient hook in the Ably Chat React SDK.
- [useChatConnection](https://ably.com/docs/chat/api/react/use-chat-connection.md): API reference for the useChatConnection hook in the Ably Chat React SDK.
- [useRoom](https://ably.com/docs/chat/api/react/use-room.md): API reference for the useRoom hook in the Ably Chat React SDK.
- [useMessages](https://ably.com/docs/chat/api/react/use-messages.md): API reference for the useMessages hook in the Ably Chat React SDK.
- [usePresence](https://ably.com/docs/chat/api/react/use-presence.md): API reference for the usePresence hook in the Ably Chat React SDK.
- [usePresenceListener](https://ably.com/docs/chat/api/react/use-presence-listener.md): API reference for the usePresenceListener hook in the Ably Chat React SDK.
- [useOccupancy](https://ably.com/docs/chat/api/react/use-occupancy.md): API reference for the useOccupancy hook in the Ably Chat React SDK.
- [useTyping](https://ably.com/docs/chat/api/react/use-typing.md): API reference for the useTyping hook in the Ably Chat React SDK.
- [useRoomReactions](https://ably.com/docs/chat/api/react/use-room-reactions.md): API reference for the useRoomReactions hook in the Ably Chat React SDK.
## 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.