import { StreamChat } from "stream-chat";
const client = StreamChat.getInstance("api_key");State Overview
Stream Chat for React Native uses the Stream Chat client to connect to and communicate with the Stream API.
See the full JavaScript client docs for client details.
Best Practices
- Use
StreamChat.getInstance()once and reuse it to avoid multiple WebSockets. - Connect the user once and disconnect explicitly when they sign out.
- Prefer UI-context helpers (e.g.,
useMessageInputContext) over direct client calls. - Keep
Chatnear the app root so contexts stay available across screens. - Avoid creating channels with only members if you need to edit membership later.
Setup
To interact with the Stream Chat API you must create a client instance and connect to the API, usually as an authenticated user.
Instantiation
StreamChat is a dependency of stream-chat-react-native and can be imported from stream-chat. Create a client with getInstance and an API key.
Usage of StreamChat.getInstance() available since stream-chat@2.12.0.
This singleton pattern lets you instantiate a single client and reuse it across the app. After calling it once, any subsequent
getInstance
call will return the initial StreamChat instance. This will prevent you from accidentally creating multiple
StreamChat instances, opening multiple WebSockets, and driving up your concurrent connections unnecessarily.
Stream Chat is backward compatible. You can still use new StreamChat() if needed.
const client = new StreamChat("api_key");Calling new StreamChat() repeatedly creates new clients and WebSocket connections when connectUser is called.
If you are using new StreamChat() you need to be vigilant about your use to ensure you are not creating multiple WebSocket connections unnecessarily.
Connecting a User
The recommended way to create a client and connect a user is with the useCreateChatClient hook:
import {
useCreateChatClient,
Chat,
OverlayProvider,
} from "stream-chat-react-native";
const App = () => {
const chatClient = useCreateChatClient({
apiKey: "YOUR_API_KEY",
userData: { id: "testUser", name: "Test User" },
tokenOrProvider: "user_token",
});
if (!chatClient) {
return null; // Show loading state
}
return (
<OverlayProvider>
<Chat client={chatClient}>{/* Your app */}</Chat>
</OverlayProvider>
);
};Alternatively, you can manually call connectUser on the client. The user_token is typically sent from your back end when a user is authenticated.
await client.connectUser(
{
id: "testUser",
name: "Test User",
},
"user_token",
);Avoid repeated connectUser calls; reconnecting without disconnecting will warn and can throw.
UI Components
After connecting a user, the UI components handle most client interactions for you.
Providing the Client to the UI
Provide the client by passing it to Chat as the client prop.
import { StreamChat } from "stream-chat";
import { Chat } from "stream-chat-react-native";
const client = StreamChat.getInstance("api_key");
export const Screen = () => (
<Chat client={client}>{/** App components */}</Chat>
);The UI components access the client via context.
If you are customizing certain components or functionality you may have to interact with the client as well.
You can access the client provided to the Chat component internally via the useChatContext.
import { useChatContext } from "stream-chat-react-native";
const { client } = useChatContext();Using UI Functions
The UI exposes functions that keep state in sync via context. See the contexts section. Use these functions to keep UI state up to date.
The sendMessage function for instance, provided by useMessageInputContext, is not the same as the sendMessage function found directly on a channel in the client.
Therefore calling channel.sendMessage(messageData) will not result in a message optimistically showing in the UI, or a failed send state, instead the message will not show until it is returned by the server.
You should not assume any function directly called on the client will result in a UI update.
The UI state is managed internally by the components and context, most client interactions require an event returned by the server to update the UI.
Accessing the Client Instance
There are multiple ways to access the client. As mentioned, you can use useChatContext inside Chat.
This works well if you can wrap your entire application in a single Chat component and have the StreamChat instance provided throughout your app via the internal context.
But if you have multiple screens that contain Chat components where a client instance is necessary you will need to access the shared instance in other ways.
You can also store the client in your own context or class, or rely on the singleton via getInstance.
Do not create and connect multiple instances using new StreamChat(), this will result in multiple StreamChat instances and opening multiple WebSocket connections.
Direct Interaction
There may be some direct interaction with the client that is required for your application. Referring to the full documentation is suggested for detailed information on client functionality. Common interactions with the client used in conjunction with the Stream Chat for React Native components have been included for convenience.
Creating a Channel
A channel must be initialized with either an id or a list of members. If you pass members, the backend auto-generates an id.
You can't add or remove members from a channel created using a members list.
/**
* Channel created using a channel id
*/
const channel = client.channel(channel_type, "channel_id", {
name: "My New Channel",
});/**
* Channel created using a members list
*/
const channel = client.channel(channel_type, {
members: ["userOne", "userTwo"],
name: "My New Channel",
});To create a channel on the server, call create or watch on the channel instance.
create will create the channel while watch will both create the channel and subscribe the client to updates on the channel.
await channel.watch();await channel.create();Querying ChannelList
To show a list of channels, see Query Channels.
Internally, a client has queryChannels API that can be used to get the list of channels.
const filter = { type: "messaging", members: { $in: ["thierry"] } };
const sort = [{ last_message_at: -1 }];
const channels = await chatClient.queryChannels(filter, sort, {
watch: true, // this is the default
state: true,
});Sending messages
To send a message in the channel, you can refer to the following guide.
Internally, a channel instance has sendMessage API that has to be called to send a message, as below:
const message = await channel.sendMessage({
text: "Hey there.",
});Disconnecting a User
To disconnect a user you can call disconnect on the client.
await client.disconnectUser();POJO
With a few of our new features, we've decided to refresh our state management and moved to a subscribable POJO with selector based system to make developer experience better when it came to rendering information provided by our StreamChat client.
This change is currently only available within StreamChat.threads, StreamChat.poll and StreamChat.polls but will be reused across the whole SDK later on.
Why POJO (State Object)
Our SDK holds and provides A LOT of information to our integrators and each of those integrators sometimes require different data or forms of data to display to their users. All of this important data now lives within something we call state object and through custom-tailored selectors our integrators can access the combination of data they require without any extra overhead and performance to match.
What are Selectors
Selectors are functions provided by integrators that run whenever state object changes. These selectors should return piece of that state that is important for the appropriate component that renders that piece of information. Selectors itself should not do any heavy data computations that could resolve in generating new data each time the selector runs (arrays and objects), use pre-built hooks with computed state values or build your own if your codebase requires it.
Rules of Selectors
- Selectors should return a named object.
const selector = (nextValue: ThreadManagerState) => ({
unreadThreadsCount: nextValue.unreadThreadsCount,
active: nextValue.active,
lastConnectionDownAt: nextvalue.lastConnectionDownAt,
});- Selectors should live outside components scope or should be memoized if it requires "outside" information (
userIdforreadobject for example). Not memoizing selectors (or not stabilizing them) will lead to bad performance as each time your component re-renders, the selector function is created anew anduseStateStoregoes through unsubscribe and resubscribe process unnecessarily.
// ❌ not okay
const Component1 = () => {
const { latestReply } = useStateStore(
thread.state,
(nextValue: ThreadState) => ({
latestReply: nextValue.latestReplies.at(-1),
}),
);
return <Text>{latestReply.text}</Text>;
};
// ✅ okay
const selector = (nextValue: ThreadState) => ({
latestReply: nextValue.latestReplies.at(-1),
});
const Component2 = () => {
const { latestReply } = useStateStore(thread.state, selector);
return <Text>{latestReply.text}</Text>;
};
// ✅ also okay
const Component3 = ({ userId }: { userId: string }) => {
const selector = useCallback(
(nextValue: ThreadState) => ({
unreadMessagesCount: nextValue.read[userId].unread_messages,
}),
[userId],
);
const { unreadMessagesCount } = useStateStore(thread.state, selector);
return <Text>{unreadMessagesCount}</Text>;
};- Break your components down to the smallest reasonable parts that each take care of the appropriate piece of state if it makes sense to do so.
Accessing Reactive State
Our SDK currently allows to access two of these state structures - in Thread and ThreadManager instances under state property.
Vanilla
import { StreamChat } from "stream-chat";
const client = new StreamChat(/*...*/);
// calls console.log with the whole state object whenever it changes
client.threads.state.subscribe(console.log);
let latestThreads;
client.threads.state.subscribeWithSelector(
// called each time theres a change in the state object
(nextValue) => ({ threads: nextValue.threads }),
// called only when threads change (selected value)
({ threads }) => {
latestThreads = threads;
},
);
// returns lastest state object
const state = client.threads.state.getLatestValue();
const [thread] = latestThreads;
// thread instances come with the same functionality
thread?.state.subscribe(/*...*/);
thread?.state.subscribeWithSelector(/*...*/);
thread?.state.getLatestValue(/*...*/);useStateStore Hook
For the ease of use - the React Native SDK comes with the appropriate state access hook which wraps StateStore.subscribeWithSelector API for the React-based applications.
import { useStateStore } from "stream-chat-react-native";
import type { ThreadManagerState } from "stream-chat";
const selector = (nextValue: ThreadManagerState) =>
({ threads: nextValue.threads }) as const;
const CustomThreadList = () => {
const { client } = useChatContext();
const { threads } = useStateStore(client.threads.state, selector);
return (
<View>
{threads.map((thread) => (
<Text key={thread.id}>{thread.id}</Text>
))}
</View>
);
};Thread and ThreadManager
One of the feature that follows the new POJO style of state management is the threads feature.
It provides a reactive state for both a single Thread (through a reactive threadInstance) and a list of threads (through StreamChat.threads).
Both states can be accessed through selectors as outlined in the examples above.
Poll and PollManager
Our new polls feature also follows the new POJO style of state management. A poll in itself is something that needs to be linked to a message in order for it to work. When a poll is created, the only way to make it visible to the users is to send it as a message. This message needs to have a poll_id attached to it and preferably no text.
You can access each poll's reactive state by getting it by ID using StreamChat.polls.fromState(<poll-id>).
Please keep in mind that message.poll is not going to be reactive, but will rather contain the raw poll data as returned by our backend.
Utility hooks
The React Native SDK provides 2 utility hooks to help with consuming the poll state. They can be found listed below:
Similarly to the threads feature, one can also directly use useStateStore and access StreamChat.polls.fromState(<poll-id>).state through custom selectors.
Both usePollStateStore and usePollState can only be used in children of a PollContext. This impediment does not exist however on useStateStore.
Due to this, all poll related components within the SDK are self-wrapped within a PollContext and require message and poll as mandatory props.
Example
import { usePollState } from "stream-chat-react-native";
const CustomPollComponent = () => {
const { name, options } = usePollState();
return (
<View>
<Text>{name}</Text>
{options.map((option) => (
<Text key={option.id}>{option.text}</Text>
))}
</View>
);
};
const PollMessage = ({ message }) => {
const { client } = useChatContext();
const pollInstance = client.polls.fromState(message?.poll_id);
return (
<PollContextProvider value={{ message, poll: pollInstance }}>
<CustomPollComponent />
</PollContextProvider>
);
};