Previously, we had implemented a chat application with simple functionality to send and receive messages in this blog. This blog is an extension of the last blog, where we will see what functions the Azure Communication Services (ACS) library provides; hence to get the data, we don't need to rely on the APIs.
ACS provides multiple methods for advanced functionalities. Thus, it helps implement all extended features without worrying about complex code.
In this blog, we will explore different methods of the ACS library that will help us to:
- Create/get/delete chat threads
- Get all participants of a particular chat thread
- Update/delete a message
- Send typing notifications/read receipts
- Subscribe to multiple chat events
All these features can be achieved in a few simple steps.
Required Data: We need the objects of ChatAsyncClient
and ChatThreadAsyncClient
classes. Refer to the previous blog to understand how to create the object of these classes.
- Chat Thread:
A. Create a chat thread: To create a thread forchatAsyncClient
object, we need to set a topic that will appear as the name of the conversation & participants list will be the list of the participants who will be part of the conversation.
B. Get all chat threads:fun createAChatThread() { val createChatThreadOptions = CreateChatThreadOptions() .setTopic("chatThreadName") .setParticipants(participantsList) .setIdempotencyToken() val createChatThreadResult = chatAsyncClient!!.createChatThread(createChatThreadOptions).get() val chatThreadProperties = createChatThreadResult.chatThreadProperties val threadId = chatThreadProperties.id }
ChatAsyncClient
will help get the list of chat threads. Iterating over the list will give theChatThreadItem
data.
C. Delete chat thread:fun getAllThreads() { val listChatThreads: PagedAsyncStream<ChatThreadItem> = chatAsyncClient!!.listChatThreads() listChatThreads.forEach(object : AsyncStreamHandler<ChatThreadItem> { override fun onNext(chatThreadItem: ChatThreadItem?) {} override fun onComplete() {} override fun onError(throwable: Throwable?) {} }) }
ChatAsyncClient
hasdeleteChatThread()
method. By sendingthreadId
, one can delete the entire chat thread.fun deleteChatThread() { val deleteChatThreadResult = chatAsyncClient?.deleteChatThread(threadId) val isDeleted = deleteChatThreadResult?.isDone }
- Get all participants added to the thread: The
ChatThreadAsyncClient
an object will help get participants added while creating a new thread.fun getParticipantsOfTheThread(){ val participantsList = PagedAsyncStream<ChatParticipant> = chatThreadAsyncClient!!.listParticipants() participantsPagedAsyncStream.forEach { chatParticipant: ChatParticipant -> val participantName = chatParticipant.displayName } }
- Update/delete a message: In the last blog, we implemented the sent and received message functionality. In this blog, I have explained how to update/delete functionality.
A. Update chat a message: ChatThreadAsyncClient object will help to update the message with the help ofchatMessageId
.
B. Delete chat message: With the help offun updateChatMessage() { val option = UpdateChatMessageOptions() option.content = "NewMessageText" chatThreadAsyncClient?.updateMessage(chatMessageId, option) }
ChatThreadAsyncClient
object, we can delete the message usingchatMessageId
.fun deleteChatMessage() { chatThreadAsyncClient?.deleteMessage(chatMessageId) }
- Send typing indicatory/read receipt:
A. Send typing notification: This function will be helpful to notify the other participants about the typing status.
B. Send Read receipt: This function is used to send read receipts to the thread.fun sendTypingNotification() { chatThreadAsyncClient.sendTypingNotification().get(); }
fun sendReadReceipt() { chatThreadAsyncClient?.sendReadReceipt(chatMessageId)?.get(); }
- Subscribe to multiple chat events:
This feature lets clients listen to Communication Services for real-time updates and incoming messages to a chat thread without polling the APIs. The client app can subscribe to events such asCHAT_MESSAGE_RECEIVED
,CHAT_MESSAGE_EDITED
,CHAT_MESSAGE_DELETED
. Refer to this link for more details.chatAsyncClient!!.addEventHandler(ChatEventType.YOUR_EVENT_TYPE) { payload: ChatEvent? -> val chatMessageReceivedEvent = payload as ChatMessageReceivedEvent? }
Note: If the app is in the background, the client will not be notified of any incoming messages and other chat events. ACS has provided the option of Push notifications to listen to incoming events while the app is in the background, but it is currently supported only for Android SDK version 1.1.0-beta.4.
Hope this article helped you explore the ACS library's multiple features.