|
| 1 | +--- |
| 2 | +id: offline-support |
| 3 | +sidebar_position: 6 |
| 4 | +title: Offline Support |
| 5 | +--- |
| 6 | + |
| 7 | +React Native Chat SDK provides OOTB support for offline mode, which means user will be able to access and interact |
| 8 | +with chat even when the network is off. This is an opt-in feature that needs to be explicitely enabled on application |
| 9 | +level. |
| 10 | + |
| 11 | +> Offline support is currently not supported for Expo package (stream-chat-expo). |
| 12 | +
|
| 13 | +## Features |
| 14 | + |
| 15 | +- ✅ Access to channel list and message list without network |
| 16 | +- ✅ Faster loading of chat since chat gets loaded from offline storage first before loading data from network |
| 17 | +- ✅ Syncing the local database using websocket events and sync api (once network is recovered) |
| 18 | +- ❌ Caching of image attachments |
| 19 | +- ❌ Optimistic updates to database on user action in offline mode e.g., send message, add reaction etc |
| 20 | + |
| 21 | +## How To Enable Offline Support |
| 22 | + |
| 23 | +- **Upgrade stream-chat dependency (optional)** |
| 24 | + |
| 25 | + If you have installed `stream-chat` dependency explicitely on your application, then upgrade it to v7 |
| 26 | + |
| 27 | + ```bash |
| 28 | + |
| 29 | + ``` |
| 30 | + |
| 31 | +- **Add `react-native-quick-sqlite` dependency** |
| 32 | + |
| 33 | + ```bash |
| 34 | + yarn add react-native-quick-sqlite |
| 35 | + npx pod-install |
| 36 | + ``` |
| 37 | + |
| 38 | +- **Ensure unique instance of Chat component** |
| 39 | + |
| 40 | + Until v4, you could provide separate `Chat` component for each usage of `Channel` component or `ChannelList` component. |
| 41 | + But from v5, it is necessary that you provide only one instance of `Chat` component within your application. |
| 42 | + This component needs to be a parent for all the chat related components such as `ChannelList`, `Channel` or `Thread`. |
| 43 | + |
| 44 | +- **Do not wait for `connectUser` call to succeed** |
| 45 | + |
| 46 | + It is important that you call `connectUser` method on chat client, before you render Chat components. |
| 47 | + But you don't need to wait for `connectUser` to succeed before rendering Chat components. This is to ensure: |
| 48 | + |
| 49 | + - Chat components have access to current user information, which is important to store/access offline data. |
| 50 | + - In case or slow or no network, Chat components will still load the chat data without waiting for connectUser to succeed. |
| 51 | + |
| 52 | + ```tsx {7,8,16} |
| 53 | + const chatClient = StreamChat.getInstance('API_KEY'); |
| 54 | + const App = () => { |
| 55 | + const [isClientReady, setIsClientReady] = useState(false); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + const startChat = async () => { |
| 59 | + const connectPromise = chatClient.connectUser(user, tokenOrTokenProvider); |
| 60 | + setIsClientReady(true); // this allows components to render |
| 61 | + await connectPromise(); |
| 62 | + // Any other post-connectUser logic you may have goes here. |
| 63 | + }; |
| 64 | + |
| 65 | + startChat(); |
| 66 | + }, []); |
| 67 | + |
| 68 | + if (!isClientReady) return null; // or some loading indicator; |
| 69 | + |
| 70 | + return ( |
| 71 | + <Chat client={chatClient} enableOfflineSupport> |
| 72 | + ... |
| 73 | + </Chat> |
| 74 | + ); |
| 75 | + }; |
| 76 | + ``` |
| 77 | + |
| 78 | +- **Add `enableOfflineSupport` prop on Chat component** |
| 79 | + |
| 80 | + ```tsx |
| 81 | + import { Chat } from 'stream-chat-react-native'; |
| 82 | + |
| 83 | + <Chat client={chatClient} enableOfflineSupport> |
| 84 | + ... |
| 85 | + </Chat>; |
| 86 | + ``` |
| 87 | + |
| 88 | +- **Reset the database when signing out the user** |
| 89 | + |
| 90 | + Since SDK doesn't handle the app level authentication logic, its application's responsibility |
| 91 | + to ensure resetting database when user gets logged out. This should be generally done before you |
| 92 | + call `client.disconnectUser()` |
| 93 | + |
| 94 | + ```jsx |
| 95 | + import { QuickSqliteClient } from 'stream-chat-react-native'; |
| 96 | + |
| 97 | + // Sign out logic |
| 98 | + QuickSqliteClient.resetDB(); |
| 99 | + chatClient.disconnectUser(); |
| 100 | + ``` |
0 commit comments