This service provides the main functionality of the chat SDK.
synchronize
This method synchronises the conversation store.
It does this through the following process:
- Obtains a list of conversations that this user is a participant in.
- Iterates over the list of conversations, comparing what is stored locally in conversation store.
- Local conversations are added or updated as necessary.
There is a concept of lazy loading with a configurable threshold - use the withLazyLoadThreshold()
method on the ComapiChatConfig
interface to specify the desired value.
The SDK orders all local conversations by date and only synchronise the first n
conversations specified by the lazyLoadThreshold
. If a conversation that doesn't get synchronised subsequently receives an event, such as a new message, it gets synchronised at that point. It also gets synchronised with a call to getConversationInfo()
.
getPreviousMessages
The SDK only retrieves the last page of messages for a conversation when it synchronises. To implement an infinite scroll-style interface you call this method. It loads another page of historic messages.
The page size is configurable - use the withMessagePageSize()
method on the ComapiChatConfig
interface to specify the desired value.
The method returns a Boolean result through a promise. If you’re at the beginning of the conversation, meaning you have all the messages, and you call this method, the Boolean result is false.
getConversations
Method to return a list of all the conversations that a user is part of.
chatClient.messaging.getConversations(conversationId)
.then(conversations => {
console.log("got conversations", conversations);
});
getConversationInfo
Method to get conversation information to render in a detailed view. It returns an object containing messages, conversation, and participant data.
The conversation is synchronised if necessary during this call.
chatClient.messaging.getConversationInfo(conversationId)
.then(info => {
console.log("got info", info);
});
an IChatConversationInfo
object is returned through a Promise:
export interface IChatConversationInfo {
conversation: IChatConversation;
messages?: IChatMessage[];
participants?: IConversationParticipant[];
}
sendTextMessage
Method to send a simple text message to a conversation.
chatClient.messaging.sendTextMessage("Hello world")
.then(result => {
console.log("sent!");
});
sendMessage
Method to send a message to a conversation. This message needs to be created with the MessageBuilder
helper object. This gives you full control of the message and its constituent parts.
import { MessageBuilder } from '@comapi/sdk-js-chat';
let message = new MessageBuilder().withText("Hello world");
chatClient.messaging.sendMessage("your conversation id", message)
.then(result => {
console.log("sent!");
});
var message = new COMAPI_CHAT.MessageBuilder().withText("Hello world");
chatClient.messaging.sendMessage("your conversation id", message)
.then(function(result){
console.log("sent!");
});
sendAttachment
Method to create an attachment through the content API. Create message part(s) using the attachment URL and the optional message and send it.
var contentData = COMAPI_CHAT.ContentData.createFromFile(attachment);
chatClient.messaging.sendAttachment(conversationId, contentData)
.then(function (result) {
console.log("sent!");
});
messageFromContentData
Method to create an attachment through the content API and create a message part. An optional text message can also be specified to accompany the attachment.
The variable
attachment
is a File object.
var contentData = COMAPI_CHAT.ContentData.createFromFile(attachment);
chatClient.messaging.messageFromContentData(contentData)
.then(function (message) {
chatClient.messaging.sendMessage(conversationId, message)
});
Additional methods
- markMessagesAsRead
Method to mark the specified messages as read.
- markAllMessagesAsRead
Go through all the messages we have in the store for the given conversation ID and mark them as read.
- isMessageRead
Method to determine whether a message has been read. If aProfileId
is specified, that is checked, otherwise we check the current user.
- createConversation
Method to create a conversation. Creates a conversation in the backend and then inserts it into the local conversation store.
- updateConversation
Method to update a conversation.
- deleteConversation
Method to delete a conversation. Deletes conversation in the backend and then removes from local conversation store.
- getParticipantsInConversation
Method to get the participants of a conversation.
- addParticipantsToConversation
Method to add participants to a conversation.
- deleteParticipantsFromConversation
Method to delete participants from a conversation.
- sendIsTyping
Function to send an is-typing event.
- sendIsTypingOff
Function to send an is-typing off event.