Skip to content

Commit 8a1e69b

Browse files
Adjust Getting Started & add Client and User content
1 parent 224ff76 commit 8a1e69b

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

docusaurus/docs/React/basics/getting-started.mdx

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ This section provides a high level overview of the library setup, core component
1010
Before starting, make sure you have installed `stream-chat-react` (and `stream-chat`), as directed in the
1111
[Installation](./installation.mdx) section.
1212

13+
You'll also need to [register](https://getstream.io/chat/trial/) and create a free tier (for up to 25 MAU) Stream application to access credentials from which you'll be able to [generate a token](https://getstream.io/chat/docs/react/token_generator/) for a user which can access your chat application.
14+
1315
The example below is all the code you'll need to launch a fully functioning chat experience. The [`Chat`](../components/core-components/chat.mdx) and [`Channel`](../components/core-components/channel.mdx) components are React context providers that pass a variety of values to their children, including UI components, stateful data, and action handler functions.
1416

1517
```jsx
@@ -60,7 +62,7 @@ const App = () => {
6062

6163
## Chat Client & Connecting User
6264

63-
To communicate with the Stream Chat API the SDK requires a connected client. The hook mentioned in the code above (`useCreateChatClient`) handles client instantiation, establishes proper connection and handles cleanups and disconnects for you. If you wish to have more control over how all of the previously mentioned is being handled see [Client and User](../guides/client-and-user.mdx) guide.
65+
To communicate with the Stream Chat API the SDK requires a client with an established connection. The hook mentioned in the code above (`useCreateChatClient`) handles client instantiation, establishes proper connection and handles cleanups and disconnects for you. If you wish to have more control over how all of the previously mentioned is being handled see [Client and User](../guides/client-and-user.mdx) guide.
6466

6567
## Creating a Channel
6668

docusaurus/docs/React/guides/client-and-user.mdx

+42
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,45 @@ id: client-and-user
33
title: Client and User
44
keywords: [client, user, connection, api]
55
---
6+
7+
:::note
8+
The recommended way of connecting your chat application to the Stream Chat API is through the use of `useCreateChatClient` hook.
9+
:::
10+
11+
## Client Instance
12+
13+
You can create an instance with the `new` keyword or through the use of static method `getInstance` - the latter will create and store your instance and subsequent `getInstance` calls will return what has been stored.
14+
15+
```tsx
16+
import { Chat } from 'stream-chat-react';
17+
import { StreamChat } from 'stream-chat';
18+
19+
const client = new StreamChat(apiKey);
20+
21+
// or
22+
23+
const client = StreamChat.getInstance(apiKey);
24+
```
25+
26+
## Connecting and Disconnecting a User
27+
28+
To authenticate a user you'll need a token. Typically, you send this token from your backend to your frontend when the user logs in. See the [Tokens & Authentication](https://getstream.io/chat/docs/javascript/tokens_and_authentication) documentation to learn more about creating tokens. For our purposes here, we will assume you have created and retrieved a `token`.
29+
30+
To connect a user, call the `connectUser` method on your client instance with the user object and `token` provided as arguments. Connect the user directly after instantiating the client to establish a websocket connection with the Stream Chat API. Once the connection has been opened, your client instance will begin receiving events from the API.
31+
32+
```tsx
33+
const connectionPromise = client.connectUser(
34+
{
35+
id: 'dave-matthews',
36+
name: 'Dave Matthews',
37+
},
38+
token,
39+
);
40+
```
41+
42+
To dispose of the active connection (upon component cleanup for example) you'd call `disconnectUser` method. It's generally recommended to wait for the connection promise to resolve before disconnecting.
43+
44+
```tsx
45+
await connectionPromise;
46+
client.disconnectUser();
47+
```

0 commit comments

Comments
 (0)