How could I store one-to-one chats with Azure Communication Service?

Vincent Lauwen 85 Reputation points
2023-05-23T16:37:55.92+00:00

Hello community,

I am currently working on a chat application with Azure Communication Service that consists of an addressbook page and chats page. The addressbook contains users that you are friends with. Under each entry (friend), there is a chat button that should lead you to the chats page. The chats page consists of all your one-to-one chats and brings you to the entry with you and your friends chat.

The first time clicking this button is no problem, because no chat thread even exists. We can just create a new chat thread. Now the tricky problem, how can we first check for a chat thread that has only two participants, me and the friend. This would be really helpful if you later click the button again, you will be led to the chats page and see your one-to-one chat with previous messages.
I can't store the chat thread id in an addressbook entry. If you add your friend to your addressbook doesn't mean he/she has you in their addressbook as wel. If your friend clicks the chat button, it would not know about a chat thread id. It isn't stored in their addressbook entry which means it would create a new chat thread which means a duplicate one-to-one chat.

Are there any direction, hows or possibilities that could guide me?

Thank you for your time,

Vincent

Azure Communication Services
Azure Communication Services
An Azure communication platform for deploying applications across devices and platforms.
391 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,057 questions
0 comments No comments
{count} votes

Accepted answer
  1. brtrach-MSFT 8,066 Reputation points Microsoft Employee
    2023-05-26T03:37:52.9533333+00:00

    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) {
    
    0 comments No comments

0 additional answers

Sort by: Most helpful