please select
  • UIKit
  • SDK
  • Server APIs
Chat/
SDK/
Web/
Message/
SDK
  • Install Chat SDK
  • Initialize Chat SDK
  • Login and Logout
  • Client APIs
  • Changelog
  • Message
    • Overview
    • Send a Message
    • Receive a Message
    • Historical Message
    • Forward Messages
    • Modify a Message
    • Delete Messages
    • Clear History Message
    • Recall a Message
    • Send an Online Message
    • Message Read Receipt
    • Query Messages
    • Targeted Group Message
    • Do not Notify
    • Key-Value Extensions
    • Translation
  • Conversation
    • Overview
    • Conversation List
    • Get Conversations
    • Unread Count
    • Pin Conversations
    • Delete Conversations
    • Mark
    • Conversation Group
  • Group
    • Overview
    • Group Management
    • Group Profile
    • Group Member Management
    • Group Member Profile
    • Custom Group Attribute
    • Group Counter
  • Community Topic
    • Community Management
  • User Profile and Relationship Chain
    • User Profile
    • User Status
    • Friend Management
    • Friend Group
    • Block List
  • Guideline for Beginners
  • Console Guide
    • Creating and Upgrading an Application
    • Basic Configuration
    • Feature Configuration
    • Account Management
    • Group Management
    • Webhook Configuration
  • Product Introduction
    • Message Management
      • One-to-One Message
      • Message Storage
      • Offline Push
      • Group Message
      • Message Formats
    • Account System
      • Login Authentication
      • Online Status Management
    • Group Related
      • Group System
      • Group Management
    • User Profile and Relationship Chain
      • Profile Management
      • Relationship Chain Management
  • Purchase Guide
    • Billing Overview
    • Pricing
  • Error Codes

Receive a Message

Feature Description

Message receiving needs to be implemented through event listening.

Listening for an Event

Note
Call this API to listen for events before calling the login API in order to avoid missing the events delivered by the SDK.
API
chat.on(eventName, handler, context);
Parameter
Name
Type
Description
eventName
String
Event name. All event names are stored in the TencentCloudChat.EVENT variable. To view all the events, use console.log(TencentCloudChat.EVENT).
handler
Function
Event handling method. When an event is triggered, this handler will be called to handle it.
context
* | undefined
The context expected for handler execution
Sample
let onMessageReceived = function(event) {
// event.data - An array that stores `Message` objects - [Message]
// For details of Message, please refer to https://www.tencentcloud.com/document/product/1047/47990
const messageList = event.data;
messageList.forEach((message) => {
if (message.type === TencentCloudChat.TYPES.MSG_TEXT) {
// Text message
} else if (message.type === TencentCloudChat.TYPES.MSG_IMAGE) {
// Image message
} else if (message.type === TencentCloudChat.TYPES.MSG_AUDIO) {
// Audio message
} else if (message.type === TencentCloudChat.TYPES.MSG_VIDEO) {
// Video message
} else if (message.type === TencentCloudChat.TYPES.MSG_FILE) {
// File message
} else if (message.type === TencentCloudChat.TYPES.MSG_CUSTOM) {
// Custom message
} else if (message.type === TencentCloudChat.TYPES.MSG_MERGER) {
// Merged message
} else if (message.type === TencentCloudChat.TYPES.MSG_LOCATION) {
// Geographical location message
} else if (message.type === TencentCloudChat.TYPES.MSG_GRP_TIP) {
// Group tip message
} else if (message.type === TencentCloudChat.TYPES.MSG_GRP_SYS_NOTICE) {
// Group system notification
}
});
};
chat.on(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);

Canceling Event Listening

API
chat.off(eventName, handler, context);
Parameter

Name
Type
Description
eventName
String
Event name. All event names are stored in the TencentCloudChat.EVENT variable. To view all the events, use console.log(TencentCloudChat.EVENT).
handler
Function
Event handling method. When an event is triggered, this handler will be called to handle it.
context
* | undefined
The context expected for handler execution
Sample
let onMessageReceived = function(event) {
// event.name - TencentCloudChat.EVENT.MESSAGE_RECEIVED
// event.data - An array that stores `Message` objects - [Message]
};
chat.off(TencentCloudChat.EVENT.MESSAGE_RECEIVED, onMessageReceived);