ConnectyCube / connectycube-reactnative-samples

Chat and Video Chat code samples for React Native, ConnectyCube
https://connectycube.com
Apache License 2.0
124 stars 111 forks source link

Chat loads very slow - performance issue ?? #228

Closed aravi365 closed 3 years ago

aravi365 commented 3 years ago

My code is something like this below. The chat loading takes a minimum of 3 4 seconds to load and display the messages inside the dialog window.

...
if (!ConnectyCube?.chat?.isConnected) {
    // alert('Not connected');
    try {
      ConnectyCube.init(CREDENTIALS, CONFIG);

      let userCredentials = {login: email, password: 'password123'};

      ConnectyCube.createSession(userCredentials)
        .then(async (session) => {
          await ConnectyCube.chat.connect({
            userId: session.user.id,
            password: 'password123',
          });

          let msgs = await messageListing(dialId, recipId);

          store.dispatch(otherUserProfileActions.storeMessages(msgs.items));
          store.dispatch(otherUserProfileActions.setChatLoader(false));

          ConnectyCube.chat.onMessageListener = onMessage;
          function onMessage(userId, message) {

            const isMessageExist = isMessageExistById(message.id);
            console.log('IS MESSAGE EXIST?', isMessageExist);

            let newMsg = {
              message: message.body,
              sender_id: Number(message.extension.sender_id),
              date_sent: Number(message.extension.date_sent),
              _id: message.id,
            };
            const dialogId = dialId
              ? dialId
              : store.getState().otherUserProfileReducer.dialog.dialogueId;

            if (!isMessageExist) {
              message.dialog_id == dialogId
                ? store.dispatch(otherUserProfileActions.pushMessage(newMsg))
                : console.log('another chat');
              // resetCounter();
            }
          }

        })
        .catch((error) => {

          store.dispatch(otherUserProfileActions.setChatLoader(false));
        });
    } catch (err) {
      store.dispatch(otherUserProfileActions.setChatLoader(false));
    }
  } else {
    // alert('connected');

    let msgs = await messageListing(dialId, recipId);

    store.dispatch(otherUserProfileActions.storeMessages(msgs.items));
    store.dispatch(otherUserProfileActions.setChatLoader(false));

    ConnectyCube.chat.onMessageListener = onMessage;
    function onMessage(userId, message) {
      const isMessageExist = isMessageExistById(message.id);

      let newMsg = {
        message: message.body,
        sender_id: Number(message.extension.sender_id),
        date_sent: Number(message.extension.date_sent),
        _id: message.id,
      };
      const dialogId = dialId
        ? dialId
        : store.getState().otherUserProfileReducer.dialog.dialogueId;
      // console.log('DIALOG CHECK', message);

      if (!isMessageExist) {
        message.dialog_id == dialogId
          ? store.dispatch(otherUserProfileActions.pushMessage(newMsg))
          : console.log('another chat');
        // resetCounter();
      }
    }
....

Also, the init process is also taking 4 5 seconds. Is there any optimal way to solve this issue with minimal delays for a smoother functioning of the chat system inside our project.