Suggestions

close search

Messages

Once authenticated, users can send or receive messages such as text, photos, videos, audio, locations, contacts... In order to send a message containing media content (photos, videos, audio, etc.), first you need to upload media content to your server. Then, you use Stringee SDK to send the uploaded URL.

1. Create a message

Create a message in JSON format:

    // Text message
    var txtMsg = {
      type: 1,
      convId: "YOUR_CONVERSATION_ID",
      message: {
        content: 'Hello',
        metadata: {
          key: 'value'
        }
      }
    };

In which:

  message: {

    // Audio
    audio: {
      duration: // number type, value in milliseconds.
      filePath: // audio file's url.
    },

    // Contact
    contact: {
      vcard: // contact as vcard, string value.
    }

    // File
    file: {
      filePath: // file's url.
      filename: ,
      length: // number type, number of bytes
    }

    // Location
    location: {
      lat: // number type, latitude
      lon: // number type, longitude
    }

    // Photo
    photo: {
      filePath: // photo file's url.
      thumbnail: // thumbnail file's url.
      ratio: // number type
    }

    // Text
    content: ''

    // Video
    video: {
      filePath: // photo file's url.
      thumbnail: // thumbnail file's url.
      ratio: // number type
      duration: // number type, value in milliseconds.
    }

    // Sticker
    sticker: {
      name: // string type
      category: // string type
    }
  }

Ex: Location message.

    var locationMsg = {
      convId: 'ABC',
      type: 9, // location message type
      message: {
        location: {
          lat: 123
          lon: 456
        },
        metadata: {
          key: 'value'
        }
      },
    }

2. Send a message

Send a Message by calling:

    var txtMsg = {
      type: 1,
      convId: "YOUR_CONVERSATION_ID",
      message: {
        content: 'Hello'
      }
    };

    stringeeChat.sendMessage(txtMsg, function (status, code, message, msg) {
      console.log(status + code + message + "msg result " + JSON.stringify(msg));
    });

3. Get Messages

Messages are stored on Stringee server. You can get messages as follows:

3.1. Get last messages

Get the latest messages from the Stringee server by calling:

    var convId = 'YOUR_CONVERSATION_ID';
    var count = 50;
    var isAscending = false;
    stringeeChat.getLastMessages(convId, count, isAscending, function (status, code, message, msgs) {
      console.log('status:' + status + ' code:' + code + ' message:' + message + ' conv:' + JSON.stringify(msgs));
    });

In which:

3.3. Get messages after

Get a list of messages tied to a conversation from the server of which sequence is greater than seq by calling:

    var convId = 'YOUR_CONVERSATION_ID';
    var count = 50;
    var isAscending = false;
    var sequence = 10;
    stringeeChat.getMessagesAfter(convId, sequence, count, isAscending, function (status, code, message, msgs) {
      console.log('status:' + status + ' code:' + code + ' message:' + message + ' conv:' + JSON.stringify(msgs));
    });

In which:

3.4. Get messages before

Get a list of messages tied to a conversation from the server of which sequence is smaller than seq by calling:

    var convId = 'YOUR_CONVERSATION_ID';
    var count = 50;
    var isAscending = false;
    var sequence = 10;
    stringeeChat.getMessagesBefore(convId, sequence, count, isAscending, function (status, code, message, msgs) {
      console.log('status:' + status + ' code:' + code + ' message:' + message + ' conv:' + JSON.stringify(msgs));
    });

In which:

4. Message statuses

Stringee keeps track of the delivery status of each message to the recipient. There are five possible statuses: INITIALIZED, SENDING, SENT, DELIVERED, READ. Stringee automatically updates the recipient status to SENT and DELIVERED. The only update your app can perform is marking a conversation's messages as read:

    var convId = 'YOUR_CONVERSATION_ID';
    stringeeChat.markConversationAsRead(convId, (status, code, message) => {
      console.log("status-" + status + " code-" + code + " message-" + message);
    });

Stringee will notify you of the status update of a message via the onObjectChange event of StringeeClient.

5. Delete message

Delete a message by calling:

    var convId = 'YOUR_CONVERSATION_ID';
    var msgId = 'YOUR_MESSAGE_ID';

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

6. Pin message

In order to pin a message in a conversation, call the following function:

    var convId = 'YOUR_CONVERSATION_ID';
    var msgId = 'YOUR_MESSAGE_ID';

    stringeeChat.pinMessage(convId, msgId, isPin, function (status, code, message) {
        console.log('status:' + status + ' code:' + code + ' message:' + message);
    });

In which:

Other participants in conversation will receive an event, implement the following event to listen:

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

7. Edit message

You can only text message. Therefore, call the following function to edit a message:

    var convId = 'YOUR_CONVERSATION_ID';
    var msgId = 'YOUR_MESSAGE_ID';
    var chatContent = "New text";

    stringeeChat.editMessage(convId, msgId, chatContent, function (status, code, message) {
        console.log('status:' + status + ' code:' + code + ' message:' + message);
    });

In which:

Other participants in conversation will receive an event, implement the following event to listen:

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

8. Revoke message

Revoke a message by calling the following function:

    var convId = 'YOUR_CONVERSATION_ID';
    var msgId = 'YOUR_MESSAGE_ID';

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

In which:

Other participants in the conversation will receive an event. Implement the following event to listen:

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