Skip to main content
The message list draws every message with a bubble chosen by the message’s category and type. You can replace how one kind is drawn — a built-in such as text or image, or a custom message type of your own — and keep the Kit’s rendering for everything else. The two toolkits use different names for this:
  • SwiftUI (CometChatSwiftUI) uses a BubbleFactory. You write a struct that names a MessageKind and returns SwiftUI views.
  • UIKit (CometChatUIKit) uses a CometChatMessageTemplate. You build a template with a category, a type and closures that return UIViews.

When to Use This

  • Draw a custom message type — a contact card, an order receipt, a location.
  • Replace how a built-in type is drawn — text, image, file …
  • Replace the whole row rather than only the bubble’s content.
  • Change one region of a bubble — the avatar, the sender name, the status line, the reactions — for one kind only.
To change a region for every kind, use the message list’s own view slots instead — see Message List. To change colours and fonts without replacing views, see Message Bubble Styling.

Prerequisites

  • The CometChatSwiftUI or CometChatUIKit product added from https://github.com/cometchat/cometchat-uikit-ios.git (from 6.0.0-beta.1, iOS 18).
  • The Kit initialised and a user logged in.
  • A CometChatMessageList on screen.

Quick Start

This example draws a custom message of type contact as a card with a name and a phone number.
Write a factory: name the kind, and return the content.
ContactBubbleFactory.swift
Register it on the list, and ask the list to fetch your type:
ChatScreen.swift
The request builder replaces the Kit’s request whole. The list adds the user or group; set the types, categories, page size, hideReplies and — for a thread — the parent message id yourself. Live messages are filtered by the same types, so a type missing here never shows.

Core Concepts

The key is category + type

A message is matched to a factory or template by its category and type — the SDK’s message / text, custom / extension_poll, or custom / your own type. One is kept per kind, and the last registered wins, so registering one for a built-in kind replaces the Kit’s bubble for that kind.
The key is the factory’s kind, a MessageKind. The built-in kinds are constants — .text, .image, .video, .audio, .voiceNote, .file, .sticker, .poll, .meeting, .document, .whiteboard, .card, .aiAssistant, .groupAction, .audioCall, .videoCall. A voice note is its own kind, .voiceNote, separate from .audio.
A message with no factory or template of yours draws with the Kit’s built-in bubble. A kind that nothing draws shows “Unsupported: category_type” — never a blank row. A deleted message always draws the built-in “This message was deleted”, whatever you register for its kind.

What each slot receives

Every slot receives the message as the Kit’s value type, ChatMessage, and its MessageAlignment — .incoming, .outgoing or .centred. A custom message’s customData is message.custom?.data, a dictionary of SuggestionItem.DataValue (.string, .int, .double, .bool, .array, .object, .null). The resolved bubble style is in the environment as \.cometChatMessageBubbleStyle.There is no create/bind split: the list asks the factory for a view each time it draws the row, and SwiftUI diffs it. A factory must be Sendable — a struct with Sendable stored properties is.

Content vs the whole bubble

Because content is required, a factory for a built-in kind replaces the built-in content. To keep it and change only one region, delegate content to the built-in factory:
Registering one for a centred kind — .groupAction, .audioCall, .videoCall — gives those rows the full bubble chrome: name, status line and reactions.

Optional view slots

Every slot is optional. Leave it out, or return nil, to keep the Kit’s view for that region.
style(for:) is asked per alignment and wins over the list’s bubble styles:
The message list’s own slots — leadingView:, headerView:, contentView: … — apply to every kind and outrank a factory’s.

Bubbles outside the list

The thread header, Message Information and Pinned Messages draw messages with the same bubbles. Hand them your factories or templates too.
  • CometChatMessageInformation, opened from the list’s Info option, gets the list’s bubbleFactories automatically.
  • CometChatThreadHeader(parentMessage:bubbleFactories:) and CometChatPinnedMessages(user:group:bubbleFactories:) take them as an argument.
  • CometChatMessageBubble(message:alignment:factory:) draws one message anywhere.

Creating a new custom message type

  1. Send the message as a CustomMessage with your own type. Send it through the Kit — not CometChat.sendCustomMessage — so the Kit’s messageSent event goes out with it.
  2. Draw it with a BubbleFactory (SwiftUI) or CometChatMessageTemplate (UIKit) whose category is custom and whose type matches exactly.
  3. Fetch it: add the type to the list’s messagesRequestBuilder.
  4. Register it on every screen that shows those messages — the list, and the thread header.
See Send a Message for custom messages in the SDK. The message composer has no built-in way to send your type — add an attachment option or an auxiliary button that sends it; see Message Composer.

Next Steps

Message List

Where bubbles are registered, and the list-wide view slots

Message Bubble Styling

Style bubbles without replacing views

Threaded Messages Header

The parent message, drawn with your bubbles

Upgrading from v5

v5 templates in v6, and what replaced the DataSource chain