|
1 |
| -# Stream Chat JS |
2 |
| - |
3 |
| -[](https://github.com/GetStream/stream-chat-js/actions) |
4 |
| - |
5 |
| -[](https://www.npmjs.com/package/stream-chat) |
6 |
| - |
7 |
| -stream-chat-js is the official JavaScript client for Stream Chat, a service for building chat applications. |
8 |
| - |
9 |
| -You can sign up for a Stream account at <https://getstream.io/chat/get_started/>. |
10 |
| - |
11 |
| -## Installation |
12 |
| - |
13 |
| -### Install with NPM |
14 |
| - |
15 |
| -```bash |
16 |
| -npm install stream-chat |
17 |
| -``` |
18 |
| - |
19 |
| -### Install with Yarn |
20 |
| - |
21 |
| -```bash |
22 |
| -yarn add stream-chat |
23 |
| -``` |
24 |
| - |
25 |
| -### Using JS deliver |
26 |
| - |
27 |
| -```html |
28 |
| -<script src="https://cdn.jsdelivr.net/npm/stream-chat"></script> |
29 |
| -``` |
30 |
| - |
31 |
| -## API Documentation |
32 |
| - |
33 |
| -Documentation for this JavaScript client are available at the [Stream Website](https://getstream.io/chat/docs/?language=js). |
34 |
| - |
35 |
| -### Typescript (v2.x.x) |
36 |
| - |
37 |
| -The StreamChat client is setup to allow extension of the base types through use of generics when instantiated. The default instantiation has all generics set to `Record<string, unknown>`. |
38 |
| - |
39 |
| -```typescript |
40 |
| -StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType> |
41 |
| -``` |
42 |
| - |
43 |
| -Custom types provided when initializing the client will carry through to all client returns and provide intellisense to queries. |
44 |
| - |
45 |
| -**NOTE:** If you utilize the `setAnonymousUser` function you must account for this in your user types. |
46 |
| - |
47 |
| -```typescript |
48 |
| -import { StreamChat } from 'stream-chat'; |
49 |
| -// or if you are on commonjs |
50 |
| -const StreamChat = require('stream-chat').StreamChat; |
51 |
| - |
52 |
| -type ChatChannel = { image: string; category?: string }; |
53 |
| -type ChatUser1 = { nickname: string; age: number; admin?: boolean }; |
54 |
| -type ChatUser2 = { nickname: string; avatar?: string }; |
55 |
| -type UserMessage = { country?: string }; |
56 |
| -type AdminMessage = { priorityLevel: number }; |
57 |
| -type ChatAttachment = { originalURL?: string }; |
58 |
| -type CustomReaction = { size?: number }; |
59 |
| -type ChatEvent = { quitChannel?: boolean }; |
60 |
| -type CustomCommands = 'imgur'; |
61 |
| - |
62 |
| -// Instantiate a new client (server side) |
63 |
| -const client = new StreamChat< |
64 |
| - ChatAttachment, |
65 |
| - ChatChannel, |
66 |
| - CustomCommands, |
67 |
| - ChatEvent, |
68 |
| - UserMessage | AdminMessage, |
69 |
| - CustomReaction, |
70 |
| - ChatUser1 | ChatUser2 |
71 |
| ->('YOUR_API_KEY', 'API_KEY_SECRET'); |
72 |
| - |
73 |
| -/** |
74 |
| - * Instantiate a new client (client side) |
75 |
| - * Unused generics default to Record<string, unknown> |
76 |
| - * with the exception of Command which defaults to string & {} |
77 |
| - */ |
78 |
| -const client = new StreamChat< |
79 |
| - {}, |
80 |
| - ChatChannel, |
81 |
| - {}, |
82 |
| - {}, |
83 |
| - UserMessage | AdminMessage, |
84 |
| - {}, |
85 |
| - ChatUser1 | ChatUser2 |
86 |
| ->('YOUR_API_KEY'); |
87 |
| -``` |
88 |
| - |
89 |
| -Query operations will return results that utilize the custom types added via generics. In addition the query filters are type checked and provide intellisense using both the key and type of the parameter to ensure accurate use. |
90 |
| - |
91 |
| -```typescript |
92 |
| -// Valid queries |
93 |
| -// users: { duration: string; users: UserResponse<ChatUser1 | ChatUser2>[]; } |
94 |
| -const users = await client.queryUsers({ id: '1080' }); |
95 |
| -const users = await client.queryUsers({ nickname: 'streamUser' }); |
96 |
| -const users = await client.queryUsers({ nickname: { $eq: 'streamUser' } }); |
97 |
| - |
98 |
| -// Invalid queries |
99 |
| -const users = await client.queryUsers({ nickname: { $contains: ['stream'] } }); // $contains is only an operator on arrays |
100 |
| -const users = await client.queryUsers({ nickname: 1080 }); // nickname must be a string |
101 |
| -const users = await client.queryUsers({ name: { $eq: 1080 } }); // name must be a string |
102 |
| -``` |
103 |
| - |
104 |
| -**Note:** If you have differing union types like `ChatUser1 | ChatUser2` or `UserMessage | AdminMessage` you can use [type guards](https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) to maintain type safety when dealing with the results of queries. |
105 |
| - |
106 |
| -```typescript |
107 |
| -function isChatUser1(user: ChatUser1 | ChatUser2): user is ChatUser1 { |
108 |
| - return (user as ChatUser1).age !== undefined; |
109 |
| -} |
110 |
| - |
111 |
| -function isAdminMessage(msg: UserMessage | AdminMessage): msg is AdminMessage { |
112 |
| - return (msg as AdminMessage).priorityLevel !== undefined; |
113 |
| -} |
114 |
| -``` |
115 |
| - |
116 |
| -Intellisense, type checking, and return types are provided for all queries. |
117 |
| - |
118 |
| -```typescript |
119 |
| -const channel = client.channel('messaging', 'TestChannel'); |
120 |
| -await channel.create(); |
121 |
| - |
122 |
| -// Valid queries |
123 |
| -// messages: SearchAPIResponse<ChatAttachment, ChatChannel, CommandTypes, UserMessage | AdminMessage, CustomReaction, ChatUser1 | ChatUser2> |
124 |
| -const messages = await channel.search({ country: 'NL' }); |
125 |
| -const messages = await channel.search({ priorityLevel: { $gt: 5 } }); |
126 |
| -const messages = await channel.search({ |
127 |
| - $and: [{ priorityLevel: { $gt: 5 } }, { deleted_at: { $exists: false } }], |
128 |
| -}); |
129 |
| - |
130 |
| -// Invalid queries |
131 |
| -const messages = await channel.search({ country: { $eq: 5 } }); // country must be a string |
132 |
| -const messages = await channel.search({ |
133 |
| - $or: [{ id: '2' }, { reaction_counts: { $eq: 'hello' } }], |
134 |
| -}); // reaction_counts must be a number |
135 |
| -``` |
136 |
| - |
137 |
| -Custom types are carried into all creation functions as well. |
138 |
| - |
139 |
| -```typescript |
140 |
| -// Valid |
141 |
| -client.setUser({ id: 'testId', nickname: 'testUser', age: 3 }, 'TestToken'); |
142 |
| -client.setUser({ id: 'testId', nickname: 'testUser', avatar: 'testAvatar' }, 'TestToken'); |
143 |
| - |
144 |
| -// Invalid |
145 |
| -client.setUser({ id: 'testId' }, 'TestToken'); // Type ChatUser1 | ChatUser2 requires nickname for both types |
146 |
| -client.setUser({ id: 'testId', nickname: true }, 'TestToken'); // nickname must be a string |
147 |
| -client.setUser({ id: 'testId', nickname: 'testUser', country: 'NL' }, 'TestToken'); // country does not exist on type ChatUser1 | ChatUser2 |
148 |
| -``` |
149 |
| - |
150 |
| -## More |
151 |
| - |
152 |
| -- [Logging](docs/logging.md) |
153 |
| -- [User Token](docs/userToken.md) |
154 |
| - |
155 |
| -## Publishing a new version |
156 |
| - |
157 |
| -Note that you need 2FA enabled on NPM, publishing with Yarn gives error, use NPM directly for this: |
158 |
| - |
159 |
| -```bash |
160 |
| -npm version bug |
161 |
| -npm publish |
162 |
| -``` |
163 |
| - |
164 |
| -## Contributing |
165 |
| - |
166 |
| -We welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our [Contributor License Agreement (CLA)](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) first. See our license file for more details. |
| 1 | +# Stream Chat JS |
| 2 | + |
| 3 | +[](https://github.com/GetStream/stream-chat-js/actions) |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/stream-chat) |
| 6 | + |
| 7 | +stream-chat-js is the official JavaScript client for Stream Chat, a service for building chat applications. |
| 8 | + |
| 9 | +You can sign up for a Stream account at <https://getstream.io/chat/get_started/>. |
| 10 | + |
| 11 | +## Installation |
| 12 | + |
| 13 | +### Install with NPM |
| 14 | + |
| 15 | +```bash |
| 16 | +npm install stream-chat |
| 17 | +``` |
| 18 | + |
| 19 | +### Install with Yarn |
| 20 | + |
| 21 | +```bash |
| 22 | +yarn add stream-chat |
| 23 | +``` |
| 24 | + |
| 25 | +### Using JS deliver |
| 26 | + |
| 27 | +```html |
| 28 | +<script src="https://cdn.jsdelivr.net/npm/stream-chat"></script> |
| 29 | +``` |
| 30 | + |
| 31 | +## API Documentation |
| 32 | + |
| 33 | +Documentation for this JavaScript client are available at the [Stream Website](https://getstream.io/chat/docs/?language=js). |
| 34 | + |
| 35 | +### Typescript (v2.x.x) |
| 36 | + |
| 37 | +The StreamChat client is setup to allow extension of the base types through use of generics when instantiated. The default instantiation has all generics set to `Record<string, unknown>`. |
| 38 | + |
| 39 | +```typescript |
| 40 | +StreamChat<AttachmentType, ChannelType, CommandType, EventType, MessageType, ReactionType, UserType> |
| 41 | +``` |
| 42 | + |
| 43 | +Custom types provided when initializing the client will carry through to all client returns and provide intellisense to queries. |
| 44 | + |
| 45 | +**NOTE:** If you utilize the `setAnonymousUser` function you must account for this in your user types. |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { StreamChat } from 'stream-chat'; |
| 49 | +// or if you are on commonjs |
| 50 | +const StreamChat = require('stream-chat').StreamChat; |
| 51 | + |
| 52 | +type ChatChannel = { image: string; category?: string }; |
| 53 | +type ChatUser1 = { nickname: string; age: number; admin?: boolean }; |
| 54 | +type ChatUser2 = { nickname: string; avatar?: string }; |
| 55 | +type UserMessage = { country?: string }; |
| 56 | +type AdminMessage = { priorityLevel: number }; |
| 57 | +type ChatAttachment = { originalURL?: string }; |
| 58 | +type CustomReaction = { size?: number }; |
| 59 | +type ChatEvent = { quitChannel?: boolean }; |
| 60 | +type CustomCommands = 'imgur'; |
| 61 | + |
| 62 | +// Instantiate a new client (server side) |
| 63 | +const client = new StreamChat< |
| 64 | + ChatAttachment, |
| 65 | + ChatChannel, |
| 66 | + CustomCommands, |
| 67 | + ChatEvent, |
| 68 | + UserMessage | AdminMessage, |
| 69 | + CustomReaction, |
| 70 | + ChatUser1 | ChatUser2 |
| 71 | +>('YOUR_API_KEY', 'API_KEY_SECRET'); |
| 72 | + |
| 73 | +/** |
| 74 | + * Instantiate a new client (client side) |
| 75 | + * Unused generics default to Record<string, unknown> |
| 76 | + * with the exception of Command which defaults to string & {} |
| 77 | + */ |
| 78 | +const client = new StreamChat< |
| 79 | + {}, |
| 80 | + ChatChannel, |
| 81 | + {}, |
| 82 | + {}, |
| 83 | + UserMessage | AdminMessage, |
| 84 | + {}, |
| 85 | + ChatUser1 | ChatUser2 |
| 86 | +>('YOUR_API_KEY'); |
| 87 | +``` |
| 88 | + |
| 89 | +Query operations will return results that utilize the custom types added via generics. In addition the query filters are type checked and provide intellisense using both the key and type of the parameter to ensure accurate use. |
| 90 | + |
| 91 | +```typescript |
| 92 | +// Valid queries |
| 93 | +// users: { duration: string; users: UserResponse<ChatUser1 | ChatUser2>[]; } |
| 94 | +const users = await client.queryUsers({ id: '1080' }); |
| 95 | +const users = await client.queryUsers({ nickname: 'streamUser' }); |
| 96 | +const users = await client.queryUsers({ nickname: { $eq: 'streamUser' } }); |
| 97 | + |
| 98 | +// Invalid queries |
| 99 | +const users = await client.queryUsers({ nickname: { $contains: ['stream'] } }); // $contains is only an operator on arrays |
| 100 | +const users = await client.queryUsers({ nickname: 1080 }); // nickname must be a string |
| 101 | +const users = await client.queryUsers({ name: { $eq: 1080 } }); // name must be a string |
| 102 | +``` |
| 103 | + |
| 104 | +**Note:** If you have differing union types like `ChatUser1 | ChatUser2` or `UserMessage | AdminMessage` you can use [type guards](https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types) to maintain type safety when dealing with the results of queries. |
| 105 | + |
| 106 | +```typescript |
| 107 | +function isChatUser1(user: ChatUser1 | ChatUser2): user is ChatUser1 { |
| 108 | + return (user as ChatUser1).age !== undefined; |
| 109 | +} |
| 110 | + |
| 111 | +function isAdminMessage(msg: UserMessage | AdminMessage): msg is AdminMessage { |
| 112 | + return (msg as AdminMessage).priorityLevel !== undefined; |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +Intellisense, type checking, and return types are provided for all queries. |
| 117 | + |
| 118 | +```typescript |
| 119 | +const channel = client.channel('messaging', 'TestChannel'); |
| 120 | +await channel.create(); |
| 121 | + |
| 122 | +// Valid queries |
| 123 | +// messages: SearchAPIResponse<ChatAttachment, ChatChannel, CommandTypes, UserMessage | AdminMessage, CustomReaction, ChatUser1 | ChatUser2> |
| 124 | +const messages = await channel.search({ country: 'NL' }); |
| 125 | +const messages = await channel.search({ priorityLevel: { $gt: 5 } }); |
| 126 | +const messages = await channel.search({ |
| 127 | + $and: [{ priorityLevel: { $gt: 5 } }, { deleted_at: { $exists: false } }], |
| 128 | +}); |
| 129 | + |
| 130 | +// Invalid queries |
| 131 | +const messages = await channel.search({ country: { $eq: 5 } }); // country must be a string |
| 132 | +const messages = await channel.search({ |
| 133 | + $or: [{ id: '2' }, { reaction_counts: { $eq: 'hello' } }], |
| 134 | +}); // reaction_counts must be a number |
| 135 | +``` |
| 136 | + |
| 137 | +Custom types are carried into all creation functions as well. |
| 138 | + |
| 139 | +```typescript |
| 140 | +// Valid |
| 141 | +client.setUser({ id: 'testId', nickname: 'testUser', age: 3 }, 'TestToken'); |
| 142 | +client.setUser({ id: 'testId', nickname: 'testUser', avatar: 'testAvatar' }, 'TestToken'); |
| 143 | + |
| 144 | +// Invalid |
| 145 | +client.setUser({ id: 'testId' }, 'TestToken'); // Type ChatUser1 | ChatUser2 requires nickname for both types |
| 146 | +client.setUser({ id: 'testId', nickname: true }, 'TestToken'); // nickname must be a string |
| 147 | +client.setUser({ id: 'testId', nickname: 'testUser', country: 'NL' }, 'TestToken'); // country does not exist on type ChatUser1 | ChatUser2 |
| 148 | +``` |
| 149 | + |
| 150 | +## More |
| 151 | + |
| 152 | +- [Logging](docs/logging.md) |
| 153 | +- [User Token](docs/userToken.md) |
| 154 | + |
| 155 | +## Publishing a new version |
| 156 | + |
| 157 | +Note that you need 2FA enabled on NPM, publishing with Yarn gives error, use NPM directly for this: |
| 158 | + |
| 159 | +```bash |
| 160 | +npm version bug |
| 161 | +npm publish |
| 162 | +``` |
| 163 | + |
| 164 | +## Contributing |
| 165 | + |
| 166 | +We welcome code changes that improve this library or fix a problem, please make sure to follow all best practices and add tests if applicable before submitting a Pull Request on Github. We are very happy to merge your code in the official repository. Make sure to sign our [Contributor License Agreement (CLA)](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) first. See our license file for more details. |
0 commit comments