Skip to main content

Get message history

tip
  • Whether users can retrieve group messages sent before they joined depends on the console setting. Turn on Enable pre-join history access by new member under Chat > Chat settings > Group Channels.
  • By default, users who are not group members cannot retrieve group history. To allow this, turn off Restrict chat history access to members under Chat > Chat settings > Group Channels on the console.

Get messages with a query

Use BaseChannel.createMessagesQuery() to create a paginated message query, then call loadNextPage() to load messages.

Code example

Dart
import 'package:ai_nexconn_chat_plugin/ai_nexconn_chat_plugin.dart';

final query = BaseChannel.createMessagesQuery(MessagesQueryParams(
channel: ChannelIdentifier(
channelType: ChannelType.direct,
channelId: '<target-user-id>',
),
order: TimeOrder.before,
policy: MessageOperationPolicy.localRemote,
pageSize: 20,
));

await query.loadNextPage((result, error) {
if (error == null) {
print('Loaded ${result?.data.length} messages');
print('Total matched: ${result?.totalCount}');
}
});

MessagesQueryParams

ParameterTypeDefaultDescription
channelChannelIdentifierRequiredChannel type, ID, and optional subchannel ID.
orderTimeOrderTimeOrder.beforeDirection. before: messages before the last loaded time. after: messages after.
policyMessageOperationPolicylocalLoad policy: local, remote, or localRemote.
messageTypeMessageTypeunknownFilter by message type. unknown means all types.
pageSizeint20Messages per page.

PageResult<Message>

PropertyTypeDescription
dataList<Message>Messages returned in the current page.
totalCountintTotal number of messages matched by the query.

Get a message by ID

Retrieve a single message from the local database by its local ID or server-assigned unique ID.

Dart
await BaseChannel.getMessageById(
GetMessageByIdParams(messageId: messageId),
(message, error) {
if (error == null) {
print('Message: ${message?.messageId}');
}
},
);

Get messages around a timestamp

Retrieve messages before and after a specified timestamp.

Dart
final channel = DirectChannel('<target-user-id>');
await channel.getMessagesAroundTime(
GetMessagesAroundTimeParams(
sentTime: <target-timestamp>,
beforeCount: 10,
afterCount: 10,
),
(messages, error) {
if (error == null) {
print('Messages: ${messages?.length}');
}
},
);

Get local messages by type

Use BaseChannel.createLocalMessagesByTypeQuery() with a type filter to retrieve local messages by type.