The SDK never interprets the body of a card — every accessor returns the raw
Card Schema JSON exactly as it was sent. Drawing it is your app’s
responsibility: pass that JSON to the CometChat Cards renderer for your
framework.
- React — please refer to the Card Messages guide.
- Angular — please refer to the Card Messages guide.
How Cards Arrive
There are three ways a card can reach your app. Which one you handle depends on where the card originates.| Delivery | Listener / Callback | Object | When to use |
|---|---|---|---|
| Standalone card message | MessageListener.onCardMessageReceived | CardMessage | A card sent as a complete message on its own |
| Inline card in an AI agent reply | MessageListener.onAIAssistantMessageReceived | AIAssistantMessage → getElements() | A card embedded in a persisted AI Agent reply |
| Real-time card streaming events | AIAssistantListener.onAIAssistantEventReceived | AIAssistantCardStartedEvent / ...ReceivedEvent / ...EndedEvent | Progressive rendering of a card while an AI Agent run streams |
Standalone Card Messages
A standalone card arrives as a message whose category iscard (its type is text). The SDK deserializes it into a CardMessage and delivers it through the onCardMessageReceived callback of the MessageListener.
Branch on
getCategory() === "card" to identify a card message —
getType() returns "text", the same as a plain text message.CardMessage extends BaseMessage, so all the usual metadata (getSender(), getReceiver(), getSentAt(), getConversationId(), …) is available. It adds these card-specific accessors:
| Getter | Return Type | Description |
|---|---|---|
getCard() | Object | undefined | The raw Card Schema JSON object (data.card). Returns undefined when no card is present. |
getText() | string | The plain-text content that accompanies the card (data.text), used as preview text in the conversation list and push notifications. |
getFallbackText() | string | Text to display when the card cannot be rendered (card.fallbackText). |
getTags() | Array<String> | Tags associated with the message. |
Receiving a Card Message
Register aMessageListener and implement onCardMessageReceived:
- TypeScript
- JavaScript
Rendering a Card Message
getCard() returns the raw Card Schema JSON — turning it into UI is your app’s job. Pass that JSON to the CometChat Cards renderer for your framework (see the note at the top of this page), and fall back to getFallbackText() — then getText() — when the card is absent or cannot be rendered.
A card payload is arbitrary JSON. A typical shape looks like this — but treat the structure as opaque and render whatever fields your platform sends:
Inline Cards in AI Agent Replies
When an AI Agent produces a card, it is not delivered as a separate message. Instead it is embedded, in order, inside the agent’s persisted reply — anAIAssistantMessage received through the existing onAIAssistantMessageReceived callback.
The ordered content of an assistant reply is exposed through getElements(), which returns an array of AIAssistantElement objects. Each element is a { type, value } envelope:
| Getter | Return Type | Description |
|---|---|---|
getType() | string | The element type — "text", "card", or any other type your platform emits. |
getData() | any | The element’s raw body. For "text" it is the text string; for "card" it is { card, cardId }. |
When
getElements() is non-empty, prefer it as the render source and walk the
blocks in order — a card-only reply may have an empty
getAssistantMessageData().getText(), so relying on the text alone can drop
content. getElements() returns an empty array only for messages that
predate this feature (or that carry no elements); in that case fall back to
getAssistantMessageData().getText(). The elements API is purely additive and
never breaks existing rendering.Rendering an Assistant Reply with Elements
Walk the elements in order and branch ongetType():
- TypeScript
- JavaScript
Real-Time Card Streaming Events
While an AI Agent run is in progress, cards are streamed as real-time events over theAIAssistantListener so you can render (or show a placeholder for) a card before the persisted AIAssistantMessage arrives. These events are delivered through onAIAssistantEventReceived alongside the other AI Agent stream events.
There are three card events, emitted in order per card:
| Order | Event type | Object | Meaning |
|---|---|---|---|
| 1 | card_start | AIAssistantCardStartedEvent | The agent began generating a card |
| 2 | card | AIAssistantCardReceivedEvent | The full card payload is available |
| 3 | card_end | AIAssistantCardEndedEvent | The card is finalized |
AIAssistantBaseEvent (so it carries getType(), getConversationId(), getRunId(), etc.) and adds these card-specific getters:
| Event | Extra Getters | Description |
|---|---|---|
AIAssistantCardStartedEvent | getStreamMessageId(), getCardId(), getExecutionText() | Stream/card IDs and the text shown while the card is generated |
AIAssistantCardReceivedEvent | getStreamMessageId(), getCardId(), getCard() | Stream/card IDs and the raw card payload |
AIAssistantCardEndedEvent | getStreamMessageId(), getCardId() | Stream/card IDs marking the end of the card |
getStreamMessageId() / getCardId() to correlate the three events for the same card, and getCard() on the card event to obtain the payload to render.
- TypeScript
- JavaScript
Streaming card events are the live view of a card as it is produced. Once the
run finishes, the same card is persisted inside the assistant reply and
reaches you again as an element on the
AIAssistantMessage (see Inline Cards
in AI Agent Replies). Correlate them by
cardId to avoid rendering the card twice.Removing Listeners
Always remove listeners when they are no longer needed (for example, on component unmount or page navigation) to prevent memory leaks and duplicate handling.- TypeScript
- JavaScript
Next Steps
Receive a Message
Handle real-time, unread, and historical messages of every type
AI Agents
Handle AI Agent runs, streaming events, and persisted agent replies
Real-Time Listeners
See every callback on the MessageListener and AIAssistantListener
Message Reference
Full class reference for CardMessage, AIAssistantElement, and card events