Once authenticated, users can send or receive messages, which are tied to a Conversation.
You will use StringeeChat object to perform actions related to chat.
// global
var stringeeChat;
// init
stringeeChat = new StringeeChat(stringeeClient);
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:
userIds: A set of participants' ID, for example:
var userIds = ["user1", "user2"];
options: Conversation options to use when constructing this conversation, for example:
var options = {
name: "Your conversation name",
isDistinct: false,
isGroup: true
};
callback: The result returned
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.
If you pass userIds with more than one user id, the Conversation is created as a Group Chat Conversation.
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.
If a Conversation is created as a distinct Conversation, it's unique on Stringee server. This means, 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 get resolved to the existing Conversation.
Conversations are created as non-distinct Conversations by default.
Creating a distinct Conversation by set isDistinct option:
Conversations are stored on Stringee server. You can get conversations as follows:
To get lastest conversations from Stringee server, you call 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:
To get a list of conversations which have updated time greater than a datetime from server, you call:
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:
To get a list of conversations which have updated time smaller than datetime from server, you call:
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:
Participants in a Conversation. 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));
});
To leave a Conversation, you remove yourself from the 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);
});
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.