Querying Messages

To query for a list of all messages in a channel:

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();

This method will return a LiveCollection of all messages in the specified channel. You can observe the LiveCollection in order to update your view whenever you receive new messages.

Since v1.3.0, messages can be organized in threads thanks to the parentId property.

Querying 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:

import { MessageRepository } from '@amityco/js-sdk';

MessageRepository.queryMessages({ channelId, isDeleted: false });

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

Get single 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.

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.

import { MessageRepository } from '@amityco/js-sdk';

const liveCollection = MessageRepository.queryMessages({
  channelId: 'channel1',
  parentId: 'exampleParentMessageId',
  filterByParentId: true,
});

Last updated