/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Retrieve a list of messages

Copy link

Ways to load messages are different for open channels and group channels. For open channels, use MessageListParams or PreviousMessageListQuery to retrieve messages from a certain reference point. Meanwhile, loadPrevious() and loadNext() in Message collection retrieve messages sent and received in a group channel.


Messages in open channels

Copy link

Using the getMessagesByTimestamp() method or the getMessagesByMessageId() method, you can retrieve messages before or after a reference point, which can be a specific timestamp in Unix milliseconds or a message ID in an open channel.

Those methods have two parameters, which are a reference point of message retrieval and a params parameter for MessageListParams. Under the MessageListParams object, you can specify values to properties such as previousResultSize, messageTypeFilter, and customType. The following code shows several types of parameters that you can configure to customize a message query.

JavaScriptTypeScript
try {
  const params = {
    prevResultSize: 5,
    nextResultSize: 5,
    // ...
  };
  const messages = await channel.getMessagesByTimestamp(ts, params);
} catch(e) {
  // Handle error
}
try {
  const params = {
    prevResultSize: 5,
    nextResultSize: 5,
    // ...
  };
  const messages = await channel.getMessagesByMessageId(messageId, params);
} catch(e) {
  // Handle error
}

MessageListParams

Copy link

This table only contains properties shown in the code above. To see the comprehensive list of all available methods and properties, see MessageListParams in API reference.

Property nameTypeDescription

prevResultSize

number

Specifies the number of messages to retrieve that were sent before the specified timestamp.

nextResultSize

number

Specifies the number of messages to retrieve that were sent after the specified timestamp.

If you wish to get messages using pagination instead of a time-based or ID-based reference point, use PreviousMessageListQuery and its load() method, which returns a list of BaseMessage objects. With a returned list, you can display the past messages in your UI once they are loaded. The following code is an example of retrieving previous messages in a channel.

JavaScriptTypeScript
const params = {
  limit: 20,
  reverse: false,
  messageTypeFilter: ALL,
  includeReactions: true,
  // ...
};
const query = channel.createPreviousMessageListQuery(params);
try {
  const messages = await query.load();
} catch(e) {
  // Handle error
}

PreviousMessageListQueryParams

Copy link

To see the comprehensive list of all available methods and properties, see PreviousMessageListQueryParams in API reference.

Property nameTypeDescription

limit

number

Specifies the number of results returned per call. Acceptable values are 1 to 100, inclusive. The recommended value for this property is 20.

reverse

boolean

Determines whether to sort the retrieved messages in reverse order. If set to true, messages are returned from the most recent to the oldest. (Default: false)

messageTypeFilter

MessageTypeFilter

Specifies the message type to filter the messages with the corresponding type. Acceptable values are BaseChannel.MessageTypeFilter.ALL, BaseChannel.MessageTypeFilter.USER, BaseChannel.MessageTypeFilter.FILE, and BaseChannel.MessageTypeFilter.ADMIN, or you can leave the value empty. If left empty, all message types are returned.

includeReactions

boolean

Determines whether to retrieve the information about reactions made to each message.


Messages in group channels

Copy link

A chat view of a group channel can be drawn with a MessageCollection instance. To retrieve messages within the collection, check if there are more messages to load through hasPrevious or hasNext and then call loadPrevious or loadNext, respectively. Then the previous or next page of messages is retrieved.

JavaScriptTypeScript
// Get the next page of messages.
if (collection.hasNext) {
  const messages = await collection.loadNext();
}
// Get the previous page of messages.
if (collection.hasPrevious) {
  const messages = await collection.loadPrevious();
}