forked from seox123/chatee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseChatClient.js
More file actions
41 lines (35 loc) · 839 Bytes
/
useChatClient.js
File metadata and controls
41 lines (35 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useEffect, useState } from "react";
import { StreamChat } from "stream-chat";
import {
chatApiKey,
chatUserId,
chatUserName,
chatUserToken,
} from "./chatConfig";
const user = {
id: chatUserId,
name: chatUserName,
};
const chatClient = StreamChat.getInstance(chatApiKey);
export const useChatClient = () => {
const [clientIsReady, setClientIsReady] = useState(false);
useEffect(() => {
const setupClient = async () => {
try {
await chatClient.connectUser(user, chatUserToken);
setClientIsReady(true);
console.log("Stream Chat connect user");
} catch (error) {
if (error instanceof Error) {
console.error(error.message);
}
}
};
if (!chatClient.userID) {
setupClient();
}
}, []);
return {
clientIsReady,
};
};