Suggestions

close search

Conversations

Once authenticated, users can send or receive messages, which are tied to a Conversation

Create a conversation

A Conversation is created by calling:

stringeeClient.createConversation(userIds, options).then(conversation => {
  console.log('createConversation', conversation);
}).catch(console.log);

In which:

  1. Chat 1-1

    To create a Chat 1-1 Conversation, you must pass userIds with only one user id, isGroup = false, for example:

    const userIds = ["user1"];
    const option = new ConversationOption();
    option.name = 'Your conversation name';
    option.isDistinct = false/true,
    option.isGroup = false;

    A Chat 1-1 Conversation is a distinct Conversation by default. You can not add participants to a Chat 1-1 Conversation.

    Note: If you pass userIds with more than one user id, the Conversation is created as a Group Chat Conversation.

  2. Group Chat

    To create a Group Chat Conversation, you pass isGroup = true or userIds with more than one user id:

    const userIds = ["user1", "user2", ...];
    const option = new ConversationOption();
    option.name = 'Your conversation name';
    option.isDistinct = false/true,
    option.isGroup = true;

    You can add or remove participants to/from a Conversation.

  3. Distinct Conversations

    If a Conversation is created as a distinct Conversation, it is unique on the Stringee server. This means that if multiple users independently create distinct Conversations with the same set of users, the server will automatically merge the conversations. Once a Distinct Conversation has been created between User A and User B, any further attempts by either of these users to create a new Distinct Conversation with these participants will be resolved into the existing Conversation. Conversations are created as non-distinct Conversations by default. Creating a distinct Conversation by setting the isDistinct option:

    • isDistinct = true -> Distinct Conversations.
    • isDistinct = false -> Non-Distinct Conversations.

Get Conversations

Conversations are stored on the Stringee server as well as cached on the Local Database. You can get conversations as follows:

  1. Get Local Conversations

    To get local conversations, you call:

    stringeeClient.getLocalConversations(userId, count, isAscending).then((conversations) => {
      conversations.map(conversation => {
          console.log('getLocalConversations', conversation);
      });
    }).catch(console.log);

    In which:

    • userId: Your user id.
    • count: Number of conversations.
    • isAscending: This param decide the order of the conversations you will receive, sort by updatedAt attribute of conversation.
      • isAscending = true, Ascending.
      • isAscending = false, Descending.
  2. Get Last Conversations

    Get the latest conversations from the Stringee server by calling:

    stringeeClient.getLastConversations(count, isAscending).then((conversations) => {
      conversations.map(conversation => {
          console.log('getLastConversations', conversation);
      });
    }).catch(console.log);

    In which:

    • count: Number of conversations.
    • isAscending: This param decide the order of the conversations you will receive, sort by updatedAt attribute of conversation.
      • isAscending = true, Ascending.
      • isAscending = false, Descending.
  3. Get Conversations After

    Get a list of conversations of which updated time is greater than the datetime from the server by calling:

    stringeeClient.getConversationsAfter(datetime, count, isAscending).then((conversations) => {
      conversations.map(conversation => {
          console.log('getConversationsAfter', conversation);
      });
    }).catch(console.log);

    In which:

    • datetime:A datetime.
    • count: Number of conversations.
    • isAscending: This param decide the order of the conversations you will receive, sort by updatedAt attribute of conversation.
      • isAscending = true, Ascending.
      • isAscending = false, Descending.
  4. Get Conversations Before

    Get a list of conversations of which updated time is smaller than the datetime from the server by calling:

    stringeeClient.getConversationsBefore(datetime, count, isAscending).then((conversations) => {
      conversations.map(conversation => {
          console.log('getConversationsBefore', conversation);
      });
    }).catch(console.log);

    In which:

    • datetime: A datetime.
    • count: Number of conversations.
    • isAscending: This param decide the order of the conversations you will receive, sort by updatedAt attribute of conversation.
      • isAscending = true, Ascending.
      • isAscending = false, Descending.

Participants

Any participant can add users into a Conversation or leave a Conversation. Only the participant who is an admin of the Conversation can remove other participants from the Conversation:

const userIds = ["user1", "user2"];
// Add
conversation.addParticipants(userIds).then(users => {
    console.log('addParticipants', users);
}).catch(console.log);
// Remove
conversation.removeParticipants(userIds).then(users => {
    console.log('removeParticipants', users);
}).catch(console.log);

Note: To leave a Conversation, you remove yourself from the Conversation.

Delete Conversation

In case the Conversation is Chat 1-1, you can delete the Conversation by calling:

conversation.deleteConversation().then(() => {
    console.log('deleteConversation success');
}).catch(console.log);

In case the Conversation is Group Chat, you must leave the Conversation before deleting.