Skip to content

Commit

Permalink
fix: forward StreamChat constructor options via useCreateChatClient (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela authored Aug 5, 2024
1 parent 43d1540 commit 310835d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
35 changes: 34 additions & 1 deletion docusaurus/docs/React/basics/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,40 @@ body,

## Chat Client & Connecting User

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.
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 the previously mentioned is being handled see [Client and User](../guides/client-and-user.mdx) guide.

:::important
The hook `useCreateChatClient` accepts parameter `options`. This is an object forwarded to the `StreamChat` constructor. Please make sure the `options` object is created outside the scope of the component invoking `useCreateChatClient` to prevent unnecessary re-renders and thus reconnects.

```
import {
Chat,
StreamChatOptions,
useCreateChatClient,
} from 'stream-chat-react';
const streamChatOptions: StreamChatOptions = {
timeout: 6000
}
const App = () => {
const client = useCreateChatClient({
apiKey,
options: streamChatOptions,
tokenOrProvider: token,
userData: { id: userId },
});
if (!client) return <div>Loading...</div>;
return (
<Chat client={client}>
</Chat>
);
}
```

:::

## Creating a Channel

Expand Down
12 changes: 8 additions & 4 deletions src/components/Chat/hooks/useCreateChatClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useEffect, useState } from 'react';

import {
import { StreamChat } from 'stream-chat';

import type {
DefaultGenerics,
ExtendableGenerics,
OwnUserResponse,
StreamChat,
StreamChatOptions,
TokenOrProvider,
UserResponse,
} from 'stream-chat';
Expand All @@ -14,12 +16,14 @@ import {
*/
export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGenerics>({
apiKey,
options,
tokenOrProvider,
userData,
}: {
apiKey: string;
tokenOrProvider: TokenOrProvider;
userData: OwnUserResponse<SCG> | UserResponse<SCG>;
options?: StreamChatOptions;
}) => {
const [chatClient, setChatClient] = useState<StreamChat<SCG> | null>(null);
const [cachedUserData, setCachedUserData] = useState(userData);
Expand All @@ -29,7 +33,7 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
}

useEffect(() => {
const client = new StreamChat<SCG>(apiKey);
const client = new StreamChat<SCG>(apiKey, undefined, options);
let didUserConnectInterrupt = false;

const connectionPromise = client.connectUser(cachedUserData, tokenOrProvider).then(() => {
Expand All @@ -45,7 +49,7 @@ export const useCreateChatClient = <SCG extends ExtendableGenerics = DefaultGene
console.log(`Connection for user "${cachedUserData.id}" has been closed`);
});
};
}, [apiKey, cachedUserData, tokenOrProvider]);
}, [apiKey, cachedUserData, options, tokenOrProvider]);

return chatClient;
};

0 comments on commit 310835d

Please sign in to comment.