Update a message
Update the content of a sent message. This replaces the original message content with the new content for all participants.
Update a message
Use the static BaseChannel.modifyMessage method, passing the Message object and the new content:
TypeScript
import { DirectChannel, BaseChannel, Message, TextMessageContent } from '@nexconn/chat';
const channel = new DirectChannel('<target-user-id>');
// First retrieve the message object you want to modify
const query = BaseChannel.createMessagesQuery({
channelIdentifier: channel.identifier,
startTime: Date.now(),
pageSize: 20,
isAscending: false,
});
const { code, data: page } = await query.loadNextPage();
if (code === 0 && page.data.length > 0) {
const message = page.data[0] as Message<TextMessageContent>;
const { code: modifyCode } = await BaseChannel.modifyMessage<TextMessageContent>({
messageId: message.messageId,
channelIdentifier: channel.identifier,
content: { text: 'Updated message content' }, // Plain content object
});
if (modifyCode === 0) {
console.log('Message updated');
} else {
console.log('Update failed. Code:', modifyCode);
}
}
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
messageId | string | Yes | The server-assigned message ID of the message to update |
channelIdentifier | ChannelIdentifier | Yes | The identifier of the channel the message belongs to |
content | T | Yes | The new message content as a plain object |
Handle message update notifications
When a message is updated, all connected users receive the updated message through the message listener:
TypeScript
import { NCEngine, MessageHandler } from '@nexconn/chat';
NCEngine.addMessageHandler('modify-handler', new MessageHandler({
onMessagesModified({ messages }) {
messages.forEach((message) => {
console.log('Message updated:', message.messageId, message.content);
});
},
}));
info
- Message updates are supported in direct channels, group channels, and community channels.
- The update is applied to the server-side message history.
- Content moderation (profanity filter, IM content moderation) is applied to the updated content as well.