Query and Filter 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.
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!)
The overall rule for the
isDeleted
parameter is as following:isDeleted = undefined
: both deleted and not deleted messages will be returnedisDeleted = true
: only deleted messages will be returnedisDeleted = 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 typeAmityMessage.DataType.TEXT
for text messagesAmityMessage.DataType.IMAGE
for image messagesAmityMessage.DataType.FILE
for file messagesAmityMessage.DataType.AUDIO
for audio messagesAmityMessage.DataType.VIDEO
for video messagesAmityMessage.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!)
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!)
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.By filtering messages, we can get messages that only match certain criteria:
iOS
Android
JavaScript
TypeScript
Flutter
- with the
includingTags
andexcludingTags
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
.noParent
is passed, any message will match - if
parent(id: String?)
is passed withoutid
, search all messages without a parent - if a non-zero id
id
passed: query for all messages with theparentId
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 typeAmityMessageType.text
for text messagesAmityMessageType.image
for image messagesAmityMessageType.file
for file messagesAmityMessageType.audio
for audio messagesAmityMessageType.video
for video messagesAmityMessageType.custom
for custom messages
- with the
includingTags
andexcludingTags
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 typeMessage.DataType.TEXT
for text messagesMessage.DataType.IMAGE
for image messagesMessage.DataType.FILE
for file messagesMessage.DataType.AUDIO
for audio messagesMessage.DataType.VIDEO
for video messagesMessage.DataType.CUSTOM
for custom messages
- the
tags
andexcludingTags
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
, truefilterByParentId
are passed, query for all messages without a parent. - when non-null
parentId
, truefilterByParentId
are passed: query for all messages with theparentId
as parent.
- with the
includingTags
andexcludingTags
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 typeMessage.DataType.TEXT
for text messagesMessage.DataType.IMAGE
for image messagesMessage.DataType.FILE
for file messagesMessage.DataType.AUDIO
for audio messagesMessage.DataType.VIDEO
for video messagesMessage.DataType.CUSTOM
for custom messages
- with the
includingTags
andexcludingTags
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 typeMessageDataType.TEXT
for text messagesMessageDataType.IMAGE
for image messagesMessageDataType.FILE
for file messagesMessageDataType.AUDIO
for audio messagesMessageDataType.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!)
Last modified 22d ago