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.

Android chat app in six steps using Azure Communication
Android code to implement chat functionality with the help of Azure Communication Service.

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:

  1. Create/get/delete chat threads
  2. Get all participants of a particular chat thread
  3. Update/delete a message
  4. Send typing notifications/read receipts
  5. 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.

  1. Chat Thread:

    A. Create a chat thread: To create a thread for chatAsyncClient 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.
    fun createAChatThread() {
        val createChatThreadOptions = CreateChatThreadOptions()
            .setTopic("chatThreadName")
            .setParticipants(participantsList)
            .setIdempotencyToken()
        val createChatThreadResult = chatAsyncClient!!.createChatThread(createChatThreadOptions).get()
        val chatThreadProperties = createChatThreadResult.chatThreadProperties
        val threadId = chatThreadProperties.id
    }
    
    B. Get all chat threads: ChatAsyncClient will help get the list of chat threads. Iterating over the list will give the ChatThreadItem data.
    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?) {}
        })
    }
    
    C. Delete chat thread: ChatAsyncClient has deleteChatThread() method. By sending threadId, one can delete the entire chat thread.
    fun deleteChatThread() {
        val deleteChatThreadResult = chatAsyncClient?.deleteChatThread(threadId)
        val isDeleted = deleteChatThreadResult?.isDone
    }
    
  2. 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
        }
    }
    
  3. 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 of chatMessageId.
    fun updateChatMessage() {
        val option = UpdateChatMessageOptions()
        option.content = "NewMessageText"
        chatThreadAsyncClient?.updateMessage(chatMessageId, option)
    }
    
    B. Delete chat message: With the help of ChatThreadAsyncClient object, we can delete the message using chatMessageId.
    fun deleteChatMessage() {
        chatThreadAsyncClient?.deleteMessage(chatMessageId)
    }
    
  4. Send typing indicatory/read receipt:

    A. Send typing notification: This function will be helpful to notify the other participants about the typing status.
    fun sendTypingNotification() {
        chatThreadAsyncClient.sendTypingNotification().get();
    }
    
    B. Send Read receipt: This function is used to send read receipts to the thread.
    fun sendReadReceipt() {
        chatThreadAsyncClient?.sendReadReceipt(chatMessageId)?.get();
    }
    
  5. 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 as CHAT_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.

References: