To store one-to-one chats with Azure Communication Service, you can use the Azure Cosmos DB to store the chat thread ID and the participants' IDs. You can create a new document in the Cosmos DB for each one-to-one chat thread, and store the chat thread ID and the IDs of the two participants in the document. When a user clicks on the chat button, you can query the Cosmos DB to check if there is an existing chat thread between the two users. If there is, you can retrieve the chat thread ID and use it to open the chat thread. If there isn't, you can create a new chat thread and store the chat thread ID in the Cosmos DB.
Here is an example of how you can use the Cosmos DB SDK for JavaScript to create a new document in the Cosmos DB:
const { CosmosClient } = require("@azure/cosmos");
const endpoint = "your_cosmos_db_endpoint";
const key = "your_cosmos_db_key";
const databaseId = "your_database_id";
const containerId = "your_container_id";
const client = new CosmosClient({ endpoint, key });
async function createDocument(chatThreadId, participant1Id, participant2Id) {
const container = client.database(databaseId).container(containerId);
const { resource: createdDocument } = await container.items.create({
id: chatThreadId,
participant1Id,
participant2Id
});
console.log(`Created document with id: ${createdDocument.id}`);
}
You can call this function to create a new document in the Cosmos DB for each one-to-one chat thread. You can then query the Cosmos DB to check if there is an existing chat thread between the two users:
async function getChatThreadId(participant1Id, participant2Id) {
const container = client.database(databaseId).container(containerId);
const querySpec = {
query: "SELECT * FROM c WHERE c.participant1Id = @participant1Id AND c.participant2Id = @participant2Id",
parameters: [
{
name: "@participant1Id",
value: participant1Id
},
{
name: "@participant2Id",
value: participant2Id
}
]
};
const { resources: documents } = await container.items.query(querySpec).fetchAll();
if (documents.length > 0) {