logo
On this page

Search for local messages

2026-02-06

Overview

With the ZIM SDK, you can search for local messages of individual or group conversations by keywords, user ID, and other conditions. You can also search for conversations based on local messages.

Search local messages of a specific conversation

After creating a ZIM object and logging in, call the searchLocalMessages interface, pass in the parameters conversationID, conversationType, and config, and set the search conditions (such as keywords) to get the list of local messages that meet the conditions in a specific conversation.

The list of messages that meet the conditions will be returned through the ZIMMessagesSearchedResult callback interface and classified by the corresponding conversation.

// Search for local text messages containing "zegocloud" of the last seven days from a conversation.

const endTime = Date.now();
// Calculate the timestamp seven days ago.
const startTime = endTime - 7 * 24 * 3600 * 1000;
// The conversation ID.
// In a one-to-one chat, set `conversationID` to the ID of the other user.
// In a group chat, set `conversationID` to the ID of the group.
const conversationID = 'xxxx';
const conversationType = 0;

const config: ZIMMessageSearchConfig = {
    count: 20, // The number of messages in the search result.
    order: 0, // Specify to search for messages in local storage in reverse chronological order.
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
    senderUserIDs: [],
    subMessageTypes: [],
};

zim.searchLocalMessages(conversationID, conversationType, config)
    .then(function ({ messageList }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });

Search for global local messages

After creating a ZIM object and logging in, call the searchGlobalLocalMessages interface, pass in the config parameter to set the search conditions (such as keywords), and globally search for local messages that meet the conditions.

The list of matched messages will be returned and categorized by conversation through the ZIMMessagesGlobalSearchedResult callback interface.

// Search for local text messages containing "zegocloud" of the last seven days from all conversations.

const endTime = Date.now();
// Calculate the timestamp seven days ago.
const startTime = endTime - 7 * 24 * 3600 * 1000;

const config: ZIMMessageSearchConfig = {
    count: 20, // The number of messages in the search result.
    order: 0, // Specify to search for messages in local storage in reverse chronological order.
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
    senderUserIDs: [],
    subMessageTypes: [],
};

zim.searchGlobalLocalMessages(config)
    .then(function ({ messageList }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });

Search for conversations Based on Local Messages

After creating a ZIM object and logging in, call the searchLocalConversations interface, pass in the config parameter to set the search conditions related to local messages, and globally search for conversations that meet the conditions.

The list of matched conversations will be returned through the ZIMConversationsSearchedResult callback interface.

// Search for local text messages containing "zegocloud" of the last seven days and have the result returned as a conversation list.

const endTime = Date.now();
// Calculate the timestamp seven days ago.
const startTime = endTime - 7 * 24 * 3600 * 1000;

const config: ZIMConversationSearchConfig = {
    totalConversationCount: 20, // The number of messages in the search result.
    conversationMessageCount: 3, // The latest three messages from each conversation are hit.
    nextFlag: 0,
    keywords: ['zegocloud'], // Pass in the keyword `zegocloud`. Up to five keywords can be passed in. If you pass in multiple keywords, only local messages containing all these keywords are displayed in the search result.
    messageTypes: [1], // Specify the message type to text message.
    startTime, // Specify the start time of the search.
    endTime, // Specify the end time of the search.
    senderUserIDs: [],
    subMessageTypes: [],
};

zim.searchLocalConversations(config)
    .then(function ({ conversationSearchInfoList, nextFlag }) {
        // Operation succeeded.
    })
    .catch(function (err) {
        // Operation failed.
    });

Search for conversations by conversation name

After creating a ZIM object and logging in, call the searchLocalConversations interface, passing in the parameter config, and set the search conditions related to the local conversation name to search all conversations that meet the criteria.

The list of conversations that match the criteria will be returned through the ZIMConversationsSearchedResult callback interface.

// Search for local conversations whose conversation name contains the keyword "zego" to get the corresponding conversation list

const config: ZIMConversationSearchConfig = {
    mode: 1, // Search by conversation name
    totalConversationCount: 20, // Number of search results
    keywords: ['zego'], // Set the keyword as "zego", up to 5 keywords are supported.
    nextFlag: 0,
};

zim.searchLocalConversations(config)
    .then((res: ZIMConversationsSearchedResult) => {
        // Operation succeeded.
    })
    .catch((err: ZIMError) => {
        // Operation failed.
    });

Previous

Set message extension field

Next

Respond to messages with emoticons

On this page

Back to top