Suggestions

close search

Conversations

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

1. Create StringeeChat Object

You will use StringeeChat object to perform actions related to chat.

  // global
  var stringeeChat;

  // init
  stringeeChat = new StringeeChat(stringeeClient);

2. Create a conversation

A Conversation is created by calling stringeeChat.createConversation(userIds, options, callback):

    stringeeChat.createConversation(userIds, options, (status, code, message, conv) => {
      console.log('status:' + status + ' code:' + code + ' message:' + message + ' conv:' + JSON.stringify(conv));
    });

In which:

2.1. Chat 1-1

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

    var userIds = ["user1"];
    var options = {
      name: "Your conversation name",
      isDistinct: false/true,
      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.2. Group Chat

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

    var userIds = ["user1", "user2", ...];
    var options = {
      name: "TĂȘn conversation",
      isDistinct: false/true,
      isGroup: true
    };

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

2.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 set isDistinct option:

3. Get Conversations

Conversations are stored on Stringee server. You can get conversations as follows:

3.1 Get Last Conversations

Get the latest conversations from the Stringee server by calling stringeeClient.getLastConversations(count, isAscending, callback):

  var count = 50;
  var isAscending = false;

  stringeeChat.getLastConversations(count, isAscending, function (status, code, message, convs) {
    console.log('status: ' + status + '  ' + code + message + ' convs:' + JSON.stringify(convs));
  });

In which:

3.2 Get Conversations After

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

  var datetime = 1569559556088;
  var count = 50;
  var isAscending = false;

  stringeeChat.getConversationsAfter(datetime, count, isAscending, function (status, code, message, convs) {
    console.log(status + code + message + ' convs:' + JSON.stringify(convs));
  });

In which:

3.3 Get Conversations Before

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

  var datetime = 1569559556088;
  var count = 50;
  var isAscending = false;

  stringeeChat.getConversationsBefore(datetime, count, isAscending, function (status, code, message, convs) {
    console.log(status + code + message + ' convs:' + JSON.stringify(convs));
  });

In which:

4. Participants

Any participant can add users into a Conversation or leave a Conversation. Only the participant who created the Conversation can remove other participants from the Conversation:

  // Add
  var convId = 'YOUR_CONVERSATION_ID';
  var userIds = ['name3'];
  stringeeChat.addParticipants(convId, userIds, function (status, code, message, added) {
    console.log('status:' + status + ' code:' + code + ' message:' + message + 'added: ' + JSON.stringify(added));
  });

  // Remove
  var convId = 'YOUR_CONVERSATION_ID';
  var userIds = ['name3'];
  stringeeChat.removeParticipants(convId, userIds, function (status, code, message, removed) {
    console.log('status:' + status + ' code:' + code + ' message:' + message + 'removed: ' + JSON.stringify(removed));
  });

When you remove a participant, that participant will receive an event:

    stringeeChat.on('removeParticipantFromServer', function (info) {
        console.log('removeParticipantFromServer ' + JSON.stringify(info));
    });
Note:

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

5. Update Conversation

You can update conversation's info by the following function:

    var convId = 'YOUR-CONVERSATION-ID';
    var params = {
        name: "New Name 1",
        imageUrl: "AVATAR'S URL"
    };

    stringeeChat.updateConversation(convId, params, function (status, code, message) {
        console.log('status:' + status + ' code:' + code + ' message:' + message);
    });

6. Delete Conversation

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

  var convId = 'YOUR_CONVERSATION_ID';
  stringeeChat.deleteConversation(convId, function (status, code, message) {
    console.log('status:' + status + ' code:' + code + ' message:' + message);
  });

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