Browse SDKs · WASM
SDKsWASM

Track the total unread count

Get and continuously maintain the account’s total unread message count with the WASM SDK.

Copy

getTotalUnreadMsgCount() returns a snapshot of the current account's total unread count across all conversations. Do not calculate it by adding unreadCount values from only the pages currently loaded.

const { data: totalUnread } = await openimsdk.getTotalUnreadMsgCount();
updateApplicationBadge(totalUnread);

The query itself does not trigger an event. While the application is in the foreground, this page exclusively handles CbEvents.OnTotalUnreadMessageCountChanged:

import { CbEvents } from '@openim/wasm-client-sdk';

const handleTotalUnreadChanged = ({ data }) => {
  updateApplicationBadge(data);
};

openimsdk.on(
  CbEvents.OnTotalUnreadMessageCountChanged,
  handleTotalUnreadChanged,
);

function removeTotalUnreadListener() {
  openimsdk.off(
    CbEvents.OnTotalUnreadMessageCountChanged,
    handleTotalUnreadChanged,
  );
}

Call removeTotalUnreadListener() when the component unmounts, the user signs out, or the account changes.