Logo

dev-resources.site

for different kinds of informations.

CometChat Offline Support in React Native

Published at
6/25/2022
Categories
reactnative
cometchat
cache
offline
Author
dimaportenko
Author
12 person written this
dimaportenko
open
CometChat Offline Support in React Native

When you building mobile chat app you definitely want keep your app usable while phone offline or with poor internet connection. Offline Chat Support is actually top voted feature request for CometChat Sdks. Unfortunately it's not there yet, but luckily there is undocumented functionality which allow you to implement it yourself.

Basically what we need is to store CometChat.BaseMessage as JSON string and restore message from this string back.

There is getRawMessage which return the raw JSON of the message. So to store array of the messages per group to some local storage we will do following

const setMessages = (groupId: string, messages: CometChat.BaseMessage[]) => {
    const rawMessages = messages.map(msg => msg.getRawMessage());
    const serialised = JSON.stringify(rawMessages);
    storage.set(groupId, serialised);
}

Enter fullscreen mode Exit fullscreen mode

And to restore message back we can use CometChatHelper and it's method processMessage which takes rawMessage data and return instance of TextMessage | MediaMessage | CustomMessage | BaseMessage. So to get array of messages from storage back we have to do something like this

const getMessages = (groupId: string) => {
    const serialisedMessages = storage.getString(groupId);
    if (serialisedMessages) {
      const rawMessages = JSON.parse(serialisedMessages) as string[];
      return Promise.all(
        rawMessages.map(rawMsg =>
          CometChat.CometChatHelper.processMessage(rawMsg),
        ),
      );
    }
},
Enter fullscreen mode Exit fullscreen mode

Here is my implementation of MessagesStorage with react-native-mmkv.

import {MMKV} from 'react-native-mmkv';
import {CometChat} from '@cometchat-pro/react-native-chat';

const storage = new MMKV({
  id: 'mmkv-messages-storage',
});

export const MessagesStorage = {
  setMessages: (groupId: string, messages: CometChat.BaseMessage[]) => {
    const rawMessages = messages.map(msg => msg.getRawMessage());
    const serialised = JSON.stringify(rawMessages);
    storage.set(groupId, serialised);
  },

  getMessages: (groupId: string) => {
    const serialisedMessages = storage.getString(groupId);
    if (serialisedMessages) {
      const rawMessages = JSON.parse(serialisedMessages) as string[];
      return Promise.all(
        rawMessages.map(rawMsg =>
          CometChat.CometChatHelper.processMessage(rawMsg),
        ),
      );
    }
  },
};
Enter fullscreen mode Exit fullscreen mode

Now you can enjoy of opening your chat screen with immediate list of messages cached from previous chat open.

offline Article's
30 articles in total
Favicon
Offline file uploading in Flutter
Favicon
It`s time to ditch the Thunder Client VSCode Extension! ๐Ÿ’ฅ
Favicon
Local First from Scratch - How to make a web app with local data
Favicon
How having a Data Layer simplified Offline Mode in my frontend app - Part 1
Favicon
Nesktop: Offline "Desktop" Next.js App
Favicon
Flutter:Hive With Api
Favicon
VScode For Android.
Favicon
-STORYTIME- Il tente de dรฉployer sans Internet, รงa tourne mal
Favicon
Transform your React Native app with offline audio & video downloads!
Favicon
Angular PWA & Service Workers (install app, push notifications, offline cache and updates)
Favicon
React Query Mutations Offline React-Native
Favicon
How can I play surf.jackbuehner.com offline?
Favicon
PrivateGPT - Running "ChatGPT" offline on local documents
Favicon
Epson Printer Offline Mac Fix Epson Offline on Mac
Favicon
Why Is My Printer Offline When I Print?
Favicon
CometChat Offline Support in React Native
Favicon
iOS โ€” Designing Data Layer For Offline-first Apps
Favicon
Eight Best Free Offline Android Games of 2022
Favicon
How to create a Offline Internationalization App:Source code and real app
Favicon
How to create a Offline Internationalization App:Support multiple languages
Favicon
How to create a Offline Internationalization App:Use Sqlite database
Favicon
How to create a Offline Internationalization App:Data modeling
Favicon
How to create a Offline Internationalization App:Build the project structure
Favicon
How to create a Offline Internationalization App:Technology
Favicon
โš™๏ธInstall anything without Admin rights
Favicon
Regarding cross platform offline data transfer between 2 mobile devices
Favicon
Challenges with Offline First Framework
Favicon
How to convert jpg/jpeg (images) to pdf offline?
Favicon
React Navigator Status
Favicon
Build a PWA using Workbox

Featured ones: