Links

Query and Filter Messages

Querying Messages

To query for a list of all messages in a channel:
iOS
Android
JavaScript
TypeScript
Flutter
Version 6
Version 5 (Maintained)
Version 6
Version 5 (Maintained)
import { MessageRepository } from '@amityco/js-sdk';
const liveCollection = MessageRepository.queryMessages({ channelId })
let messages = liveCollection.models;
liveCollection.on('dataUpdated', data => {
messages = data;
});
liveCollection.on('dataError', error => {
console.error(error);
});
// unobserve data changes once you are finished
liveCollection.dispose();
Since v1.3.0, messages can be organized in threads thanks to the parentId property.
Version 6
Version 5 (Maintained)
This method will return all messages in the specified channel as Stream<List<AmityMessage>>
This method will return a LiveObject of all messages in the specified channel. You can observe the LiveObject in order to update your view whenever you receive new messages.

Exclude Delete Message in Querying

A popular request was to have a isDeleted boolean filter to avoid querying deleted messages to display in a chatroom. It is now available and can be used as:
iOS
Android
JavaScript
TypeScript
Flutter
The functionality isn't supported by this SDK.
Supported ✅ (please wait while we prepare a real example!)
import { MessageRepository } from '@amityco/js-sdk';
MessageRepository.queryMessages({ channelId, isDeleted: false });
Version 6
Version 5 (Maintained)
Supported ✅ (please wait while we prepare a real example!)

Get Single Message

The overall rule for the isDeleted parameter is as following:
  • isDeleted = undefined : both deleted and not deleted messages will be returned
  • isDeleted = true : only deleted messages will be returned
  • isDeleted = false : only non deleted messages will be returned
  • with the type parameter you can filter messages according to their type
    • if no type is passed, any message will match
    • if an AmityMessage.DataType is passed, query for all messages with the specific type
      • AmityMessage.DataType.TEXT for text messages
      • AmityMessage.DataType.IMAGE for image messages
      • AmityMessage.DataType.FILE for file messages
      • AmityMessage.DataType.AUDIO for audio messages
      • AmityMessage.DataType.VIDEO for video messages
      • AmityMessage.DataType.CUSTOM for custom messages
iOS
Android
JavaScript
TypeScript
Flutter
To get a specific message:
func getParticularMessageExample() {
let messageObject = messageRepository.getMessage("messageId")
token = messageObject?.observe { message, error in
// Do something with the message
}
}
You can use the getMessage method to get a single comment. You need to pass the messageId of the requested message as the parameter.
import { MessageRepository } from '@amityco/js-sdk';
const liveObject = MessageRepository.getMessage('messageId');
let message = liveObject.model;
liveObject .on('dataUpdated', data => {
message = data;
});
liveObject .on('dataError', error => {
console.error(error);
});
// unobserve data changes once you are finished
liveObject.dispose();
The method returns a liveobject instance of a message model. It will throw an error if the passed messageId is not valid.
Supported ✅ (please wait while we prepare a real example!)

Threaded Messages

A message can be the root for a thread. To query the children of a message thread, you can add the parentId parameter in a message query, along with the filterByParentId flag.
iOS
Android
JavaScript
TypeScript
Flutter
Version 6
Version 5 (Maintained)
Supported ✅ (please wait while we prepare a real example!)
import { MessageRepository } from '@amityco/js-sdk';
const liveCollection = MessageRepository.queryMessages({
channelId: 'channel1',
parentId: 'exampleParentMessageId',
filterByParentId: true,
});
Version 6
Version 5 (Maintained)
Supported ✅ (please wait while we prepare a real example!)

Message Sorting

While the SDK will always return messages in chronological order, developers can ask for the messages to begin from the top of the recyclerview or the bottom of the recyclerview depeding on scrolling direction.
iOS
Android
JavaScript
TypeScript
Flutter
Version 6
Version 5 (Maintained)
Version 6
Version 5 (Maintained)
Supported ✅ (please wait while we prepare a real example!)
Supported ✅ (please wait while we prepare a real example!)
Supported ✅ (please wait while we prepare a real example!)
There are other situations where fetching the oldest messages is preferred, for example for archiving purposes or in community forums. (When stack from end is set to false, the list fills its content starting from the top of the view).
Make sure that stackFromEnd using obtain MessageCollection is the same as stackFromEnd of RecyclerView's LayoutManager. Otherwise it may cause a jumping issue.

Filtering Messages

By filtering messages, we can get messages that only match certain criteria:
iOS
Android
JavaScript
TypeScript
Flutter
  • with the includingTags and excludingTags parameters you can filter messages based on the tags set (or not set) in each message
  • with the messageParentFilter parameter you can filter messages according to their relationship:
    • if no .noParentis passed, any message will match
    • if parent(id: String?) is passed without id, search all messages without a parent
    • if a non-zero id id passed: query for all messages with the parentId as parent.
  • with the type parameter you can filter messages according to their type
    • if no type is passed, any message will match
    • if an AmityMessageType is passed, query for all messages with the specific type
      • AmityMessageType.text for text messages
      • AmityMessageType.image for image messages
      • AmityMessageType.file for file messages
      • AmityMessageType.audio for audio messages
      • AmityMessageType.video for video messages
      • AmityMessageType.custom for custom messages
  • with the includingTags and excludingTags parameters you can filter messages based on the tags set (or not set) in each message
  • with the parentId parameter you can filter messages according to their relationship:
    • if no parentId is passed, any message will match
    • if null parentId is passed, search for all messages without a parent
    • if a non-null parentId is passed: query for all messages with the parentId as parent
  • with the type parameter you can filter messages according to their type
    • if no type is passed, any message will match
    • if an Message.DataType is passed, query for all messages with the specific type
      • Message.DataType.TEXT for text messages
      • Message.DataType.IMAGE for image messages
      • Message.DataType.FILE for file messages
      • Message.DataType.AUDIO for audio messages
      • Message.DataType.VIDEO for video messages
      • Message.DataType.CUSTOM for custom messages
  • the tags and excludingTags parameters let you filter messages based on the tags set (or not set) in each message
  • the parentId parameter lets you filter messages by their relationship:
    • when no parentId is passed, any message will match.
    • when null parentId, true filterByParentId are passed, query for all messages without a parent.
    • when non-null parentId, true filterByParentId are passed: query for all messages with the parentId as parent.
  • with the includingTags and excludingTags parameters you can filter messages based on the tags set (or not set) in each message
  • with the parentId parameter you can filter messages according to their relationship:
    • if no parentId is passed, any message will match
    • if null parentId is passed, search for all messages without a parent
    • if a non-null parentId is passed: query for all messages with the parentId as parent
  • with the type parameter you can filter messages according to their type
    • if no type is passed, any message will match
    • if an Message.DataType is passed, query for all messages with the specific type
      • Message.DataType.TEXT for text messages
      • Message.DataType.IMAGE for image messages
      • Message.DataType.FILE for file messages
      • Message.DataType.AUDIO for audio messages
      • Message.DataType.VIDEO for video messages
      • Message.DataType.CUSTOM for custom messages
  • with the includingTags and excludingTags parameters you can filter messages based on the tags set (or not set) in each message
  • with the parentId parameter you can filter messages according to their relationship:
    • if no parentId is passed, any message will match
    • if null parentId is passed, search for all messages without a parent
    • if a non-null parentId is passed: query for all messages with the parentId as parent
  • with the type parameter you can filter messages according to their type
    • if no type is passed, any message will match
    • if an AmityMessageDataType is passed, query for all messages with the specific type
      • MessageDataType.TEXT for text messages
      • MessageDataType.IMAGE for image messages
      • MessageDataType.FILE for file messages
      • MessageDataType.AUDIO for audio messages
      • MessageDataType.CUSTOM for custom messages
iOS
Android
JavaScript
TypeScript
Flutter
Version 6
Version 5 (Maintained)
Version 6
Version 5 (Maintained)
import { MessageRepository } from '@amityco/js-sdk';
const liveCollection = MessageRepository.queryMessages({
channelId: 'channelId',
parentId: parentIdValue,
filterByParentId: true,
tags: ['summer2021']
excludeTags: ['awful_hotel']
});
Supported ✅ (please wait while we prepare a real example!)