Azure Communication Chat contains the APIs used in chat applications for Azure Communication Services.
Source code | Package (Maven) | API reference documentation | Product documentation
- An Azure account with an active subscription. Create an account for free.
- Java Development Kit (JDK) version 8 or above.
- Apache Maven.
- A deployed Communication Services resource. You can use the Azure Portal or the Azure PowerShell to set it up.
The current version of this library is 2.2.0-beta.1.
To install the Azure Communication Chat libraries for Android, add them as dependencies within your Gradle or Maven build scripts.
To import the library into your project using the Gradle build system, follow the instructions in Add build dependencies:
Add an implementation
configuration to the dependencies
block of your app's build.gradle
or build.gradle.kts
file, specifying the library's name and the version you wish to use:
// build.gradle
dependencies {
...
implementation "com.azure.android:azure-communication-chat:2.2.0-beta.1"
}
// build.gradle.kts
dependencies {
...
implementation("com.azure.android:azure-communication-chat:2.2.0-beta.1")
}
To import the library into your project using the Maven build system, add it to the dependencies
section of your app's pom.xml
file, specifying its artifact ID and the version you wish to use:
<dependency>
<groupId>com.azure.android</groupId>
<artifactId>azure-communication-chat</artifactId>
<version>2.2.0-beta.1</version>
</dependency>
A chat conversation is represented by a chat thread. Each user in the chat thread is called a participant. Participants can chat with one another privately in a 1:1 chat or huddle up in a 1:N group chat.
Once you initialized a ChatClient
and a ChatThreadClient
class, you can do the following chat operations:
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution.
When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.
endpoint = "https://Azure-Communication-Resource-Name.communications.azure.com"
User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. You generate these tokens on your server, pass them back to a client device, and then use them to initialize the Communication Services SDKs.
Learn how to generate user access tokens from User Access Tokens
The following sections provide several code snippets covering some of the most common tasks, including:
- Chat Thread Operations
- Chat Message Operations
- Chat Thread Member Operations
- Read Receipt Operations
- Typing Notification Operations
To create a chat client, you will use the Communications Service endpoint and the access token that was generated as part of pre-requisite steps. User access tokens enable you to build client applications that directly authenticate to Azure Communication Services. Once you generate these tokens on your server, pass them back to a client device. You need to use the CommunicationTokenCredential class from the Common SDK to pass the token to your chat client.
String endpoint = "https://<RESOURCE_NAME>.communcationservices.azure.com";
// Your user access token retrieved from your trusted service
String token = "SECRET";
// Initialize the chat client
final ChatClientBuilder builder = new ChatClientBuilder();
builder
.endpoint(endpoint)
.credential(new CommunicationTokenCredential(token))
ChatClient chatClient = builder.buildClient();
Use the createChatThread
method to create a chat thread.
createChatThreadOptions
is used to describe the thread request, an example is shown in the code snippet below.
- Use
topic
to give a thread topic; - Use
participants
to list the thread participants to be added to the thread;
CreateChatThreadResult
is the response returned from creating a chat thread.
It contains a getChatThread()
method which returns the ChatThread
object that can be used to get the thread client from which you can get the ChatThreadClient
for performing operations on the created thread: add participants, send message, etc.
The ChatThread
object also contains the getId()
method which retrieves the unique ID of the thread.
List<ChatParticipant> participants = new ArrayList<ChatParticipant>();
ChatParticipant firstParticipant = new ChatParticipant()
.setCommunicationIdentifier(user1)
.setDisplayName("Participant Display Name 1");
ChatParticipant secondParticipant = new ChatParticipant()
.setCommunicationIdentifier(user2)
.setDisplayName("Participant Display Name 2");
participants.add(firstParticipant);
participants.add(secondParticipant);
CreateChatThreadOptions createChatThreadOptions = new CreateChatThreadOptions()
.setTopic("Topic")
.setParticipants(participants);
CreateChatThreadResult result = chatClient.createChatThread(createChatThreadOptions);
String chatThreadId = result.getChatThreadProperties().getId();
Use deleteChatThread
method to delete a chat thread
chatThreadId
is the unique ID of the chat thread.
String chatThreadId = "Id";
chatClient.deleteChatThread(chatThreadId);
The getChatThreadClient
method returns a thread client for a thread that already exists. It can be used for performing operations on the created thread: add participants, send message, etc.
chatThreadId
is the unique ID of the existing chat thread.
String chatThreadId = "Id";
ChatThreadClient chatThreadClient = chatClient.getChatThreadClient(chatThreadId);
The getProperties
method retrieves properties of a thread from the service.
ChatThreadProperties chatThreadProperties = chatThreadClient.getProperties();
Use updateTopic
method to update a thread's topic
topic
is used to hold the new topic of the thread.
chatThreadClient.updateTopic("New Topic");
Use the sendMessage
method to sends a chat message to a chat thread identified by chatThreadId.
sendChatMessageOptions
is used to describe the chat message request, an example is shown in the code snippet below.
- Use
content
to provide the chat message content; - Use
type
to specify the chat message type; - Use
senderDisplayName
to specify the display name of the sender; - Use
metadata
to specify the message metadata;
A SendChatMessageResult
response returned from sending a chat message, it contains an id
, which is the unique ID of the message.
SendChatMessageOptions sendChatMessageOptions = new SendChatMessageOptions()
.setContent("Message content")
.setSenderDisplayName("Sender Display Name");
String chatMessageId = chatThreadClient.sendMessage(sendChatMessageOptions).getId();
The getMessage
method retrieves a chat message from the service.
chatMessageId
is the unique ID of the chat message.
String chatMessageId = "Id";
ChatMessage chatMessage = chatThreadClient.getMessage(chatMessageId);
You can retrieve chat messages using the listMessages
method on the chat thread client at specified intervals (polling).
PagedIterable<ChatMessage> chatMessages = chatThreadClient.listMessages();
chatMessages.forEach(chatMessage -> {
// You code to handle single chatMessage
});
listMessages
returns the latest version of the message, including any edits or deletes that happened to the message using .editMessage()
and .deleteMessage()
.
For deleted messages, chatMessage.getDeletedOn()
returns a datetime value indicating when that message was deleted.
For edited messages, chatMessage.getEditedOn()
returns a datetime indicating when the message was edited.
The original time of message creation can be accessed using chatMessage.getCreatedOn()
, and it can be used for ordering the messages.
listMessages returns different types of messages which can be identified by chatMessage.getType()
. These types are:
-
text
: Regular chat message sent by a thread participant. -
html
: HTML chat message sent by a thread participant. -
topicUpdated
: System message that indicates the topic has been updated. -
participantAdded
: System message that indicates one or more participants have been added to the chat thread. -
participantRemoved
: System message that indicates a participant has been removed from the chat thread.
For more details, see Message Types.
Use updateMessage
to update a chat message identified by chatThreadId and messageId.
chatMessageId
is the unique ID of the chat message.
updateChatMessageOptions
is used to describe the request of a chat message update, an example is shown in the code snippet below.
- Use
content
to provide a new chat message content;
String chatMessageId = "Id";
UpdateChatMessageOptions updateChatMessageOptions = new UpdateChatMessageOptions()
.setContent("Updated message content");
chatThreadClient.updateMessage(chatMessageId, updateChatMessageOptions);
Use updateMessage
to update a chat message identified by chatThreadId and chatMessageId.
chatMessageId
is the unique ID of the chat message.
String chatMessageId = "Id";
chatThreadClient.deleteMessage(chatMessageId);
Use listParticipants
to retrieve a paged collection containing the participants of the chat thread identified by chatThreadId.
PagedIterable<ChatParticipant> chatParticipants = chatThreadClient.listParticipants();
chatParticipants.forEach(chatParticipant -> {
// You code to handle single chatParticipant
});
Use addParticipants
method to add participants to the thread identified by threadId.
addChatParticipantsOptions
describes the request object containing the participants to be added; Use .setParticipants()
to set the participants to be added to the thread;
communicationIdentifier
, required, is the CommunicationIdentifier you've created by using the CommunicationIdentityClient. More info at: Create A User.displayName
, optional, is the display name for the thread participant.shareHistoryTime
, optional, is the time from which the chat history is shared with the participant. To share history since the inception of the chat thread, set this property to any date equal to, or less than the thread creation time. To share no history previous to when the member was added, set it to the current date. To share partial history, set it to the required date.
List<ChatParticipant> participants = new ArrayList<ChatParticipant>();
ChatParticipant firstParticipant = new ChatParticipant()
.setCommunicationIdentifier(user1)
.setDisplayName("Display Name 1");
ChatParticipant secondParticipant = new ChatParticipant()
.setCommunicationIdentifier(user2)
.setDisplayName("Display Name 2");
participants.add(firstParticipant);
participants.add(secondParticipant);
chatThreadClient.addParticipants(participants);
Use removeParticipant
method to remove a participant from the chat thread identified by chatThreadId.
identifier
is the CommunicationIdentifier you've created.
chatThreadClient.removeParticipant(identifier);
Use sendReadReceipt
method to post a read receipt event to a chat thread, on behalf of a user.
chatMessageId
is the unique ID of the chat message that was read.
String chatMessageId = "Id";
chatThreadClient.sendReadReceipt(chatMessageId);
Use listReadReceipts
to retrieve a paged collection containing the read receipts for a chat thread.
PagedIterable<ChatMessageReadReceipt> readReceipts = chatThreadClient.listReadReceipts();
readReceipts.forEach(readReceipt -> {
// You code to handle single readReceipt
});
Use sendTypingNotification
method to post a typing notification event to a chat thread, on behalf of a user.
chatThreadClient.sendTypingNotification();
In progress.
Check out other client libraries for Azure communication service