Set message extension field
Introduction
ZEGOCLOUD Instant Messaging (ZIM) SDK allows you to add an extension field to a message and send the extension field as additional message information. Message extension fields can be visible to the peer end or visible only to the local end based on the synchronization effects. A message extension field is used to present information such as the translation status and translation content of a message, and the business logic included in the message.
Implementation Steps
- Create a ZIM instance.
- Log in to the ZIM SDK.
- Construct the ZIMMessage object, and fill in the ZIMMessage field in the extendedData object as the message extension field.
- Call the sendMessage interface, pass in the ZIMMessage object, send the message and expand the fields.
// Send single chat `Text` message
var toConversationID = ''; // Other party userID
var conversationType = 0; // Session type, the value is single chat: 0, room: 1, group: 2
var config = {
priority: 1, // Set the message priority, the value is low: 1 default, medium: 2, high: 3
};
var messageTextObj = { type: 1, message: 'Text message content', extendedData: 'Extended information of the message (optional)' };
var notification = {
onMessageAttached: function(message) {
// todo: Loading
}
}
zim.sendMessage(messageTextObj, toConversationID, conversationType, config, notification)
.then(function ({ message }) {
// Sent successfully
})
.catch(function (err) {
// Failed to send
});
Configure an extension field visible only to the local end
Procedure for sending a message:
- Log in to ZIM SDK, construct a ZIMMessagefield as an extension field visible only to the local end in the ZIMMessage object.
- Call the sendMessage operation and pass in the ZIMMessage object to send the message and the extension field.
// Send the single chat `Text` message
var toConversationID = ''; // Other party userID
var conversationType = 0; // Session type, the value is single chat: 0, room: 1, group: 2
var config = {
priority: 1, // Set the message priority, the value is low: 1 default, medium: 2, high: 3
};
// 1. Configure the local extension field of a message.
var messageTextObj = { type: 1, message: 'Text message content', localExtendedData: 'Local extension information of the message' };
var notification = {
onMessageAttached: function(message) {
// todo: Loading
}
}
// 2. Send a message
zim.sendMessage(messageTextObj, toConversationID, conversationType, config, notification)
.then(function ({ message }) {
// Sent successfully
})
.catch(function (err) {
// Failed to send
});
Update the local extension field of a message
For a message that is already received or sent, you can call the updateMessageLocalExtendedData operation to update the local extension field of the message.
The following sample code shows how to update the local extension field of a message that is already received:
The following sample code shows how to update the local extension field of a message that is already received:
// Update the local extension field of a private-chat message after you receive the message.
zim.on('receivePeerMessage', function (zim, { messageList, fromConversationID }) {
console.log(messageList, fromConversationID);
messageList.forEach((message) => {
zim.updateMessageLocalExtendedData('Update the local extension field', message)
.then(function ({ message }) {
// Operation successful.
})
.catch(function (err) {
// Operation failed.
});
});
});