/ SDKs / JavaScript
SDKs
Chat SDKs JavaScript v4
Chat SDKs JavaScript
Chat SDKs
JavaScript
Version 4

Receive messages in an open channel

Copy link

Messages sent from other participants can be received through the onMessageReceived() method in the channel event handler. A BaseMessage object for each received message is one of the following three message types.

Message typeClassDescription

Text

UserMessage

A text message sent by a user

File

FileMessage

A binary file message sent by a user

Admin

AdminMessage

A text message sent by an admin through the Chat API

To register multiple concurrent handlers, pass a UNIQUE_HANDLER_ID argument as a unique identifier into the addOpenChannelHandler() method.

JavaScriptTypeScript
const channelHandler = new OpenChannelHandler({
    onMessageReceived: (channel, message) => {
        // ...
    }
});

sb.openChannel.addOpenChannelHandler(UNIQUE_HANDLER_ID, channelHandler);

Event handler for message threading

Copy link

Once a reply is created or deleted from a thread, onThreadInfoUpdated() in OpenChannelHandler is invoked. The method returns a ThreadInfoUpdateEvent object that has the latest information about the thread. This object needs to be applied to the parent message object.

Note: Like other messages, when a reply is created in a channel, the onMessageReceived() method of the channel event handler in client apps will be called.

JavaScriptTypeScript
handler.onThreadInfoUpdated = (channel, threadInfoUpdateEvent) => {
    // Look for a message that has threadInfoUpdateEvent.targetMessageId.
    // Apply the event to the message.
    message.applyThreadInfoUpdateEvent(threadInfoUpdateEvent);
};

List of parameters

Copy link
Parameter nameTypeDescription

channel

BaseChannel

Specifies the channel that has the message thread.

threadInfoUpdateEvent

ThreadInfoUpdateEvent

Specifies a ThreadInfoUpdateEvent object that has the latest information about the thread.

If a reply message is created, onMessageReceived() will be called with that reply message. A user will have to find the parent message with the parentMessageID property in that message object.

If the parent message is not in the message view stack and the user wants to get the parent message object, they can do so by calling sb.message.getMessage(params, handler) to retrieve the parent message object.

Once the reply message has been created, the onThreadInfoUpdated() event handler will be called since the thread info has been changed. To update thread information, the user should find the matching parent message with targetMessageID and use the parentMessage.applyThreadInfoUpdateEvent() method.