> ## Documentation Index
> Fetch the complete documentation index at: https://social-b97141fb-auto-generate-llmstxt.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Message Preview

> Read the latest message preview from chat channel and subchannel SDK objects.

Use message preview when your app needs a lightweight latest-message summary for a chat list, inbox row, or notification handoff. The SDK exposes preview data on channel and subchannel models after message preview is enabled for the network.

<Info>
  Message preview is configured outside these client SDK calls, typically through network settings. Client SDKs read the preview that is returned with channel or subchannel data; they do not enable the setting themselves.
</Info>

## Platform Surface

| Surface            | TypeScript                  | iOS                         | Android                          | Flutter                                |
| ------------------ | --------------------------- | --------------------------- | -------------------------------- | -------------------------------------- |
| Channel preview    | `channel.messagePreview`    | `channel.messagePreview`    | `channel.getMessagePreview()`    | `channel.messagePreview`               |
| Subchannel preview | `subChannel.messagePreview` | `subChannel.messagePreview` | `subChannel.getMessagePreview()` | Not exposed as a public preview object |
| Preview ID         | `messagePreviewId`          | `messagePreviewId`          | `getMessagePreviewId()`          | `messagePreviewId`                     |
| Preview data       | `data`, `dataType`          | `data`, `dataType`          | `getData()`, `getDataType()`     | `data`, `dataType`                     |

## Parameters

| Parameter                      | Required                   | Description                                                                                                                                 |
| ------------------------------ | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- |
| `channelId`                    | Yes                        | Channel ID used to retrieve or observe the channel that contains the preview.                                                               |
| `subChannelId`                 | Yes for subchannel preview | Subchannel ID used to retrieve or observe a subchannel preview where the platform exposes it.                                               |
| Preview setting                | Yes                        | The network must have message preview enabled before new messages produce preview data.                                                     |
| `messagePreview` null handling | Yes                        | A channel or subchannel can return no preview when no eligible message exists, preview is disabled, or the preview has not been cached yet. |

## Read Channel Preview

Read the preview from the channel object you already use for channel lists. Treat the preview as optional and render a fallback for empty channels.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { ChannelRepository } from '@amityco/ts-sdk';

  const unsubscribe = ChannelRepository.getChannel(
    channelId,
    ({ data: channelSnapshot, loading, error }) => {
      if (error) {
        handleError(error);
        return;
      }

      if (!loading && channelSnapshot?.messagePreview) {
        renderResults({
          id: channelSnapshot.messagePreview.messagePreviewId,
          dataType: channelSnapshot.messagePreview.dataType,
          data: channelSnapshot.messagePreview.data,
          userId: channelSnapshot.messagePreview.user?.userId,
        });
      }
    },
  );

  unsubscribe();
  ```

  ```swift iOS theme={null}
  token = channelRepository.getChannel(channelId).observe { liveObject, error in
      if let error {
          handleError(error)
          return
      }

      guard let preview = liveObject.snapshot?.messagePreview else { return }

      showSuccessMessage([
          "id": preview.messagePreviewId,
          "type": preview.dataType,
          "subChannelId": preview.subChannelId
      ])
  }
  ```

  ```kotlin Android theme={null}
  val currentChannel = channel ?: return
  val preview = currentChannel.getMessagePreview() ?: return

  showSuccessMessage(
      mapOf(
          "id" to preview.getMessagePreviewId(),
          "type" to preview.getDataType(),
          "subChannelId" to preview.getSubChannelId(),
      ),
  )
  ```

  ```dart Flutter theme={null}
  final fetchedChannel = await AmityChatClient.newChannelRepository()
      .getChannel(channelId);

  final preview = fetchedChannel.messagePreview;

  if (preview != null) {
    final previewId = preview.messagePreviewId;
    final previewType = preview.dataType;
    final previewData = preview.data;
  }
  ```
</CodeGroup>

## Read Subchannel Preview

Use subchannel preview when your UI lists subchannels directly. Flutter currently exposes `messagePreviewId` on `AmitySubChannel`, but not the composed `AmityMessagePreview` object.

<CodeGroup>
  ```typescript TypeScript theme={null}
  import { SubChannelRepository } from '@amityco/ts-sdk';

  const unsubscribe = SubChannelRepository.getSubChannel(
    subChannelId,
    ({ data: subChannel, loading, error }) => {
      if (error) {
        handleError(error);
        return;
      }

      if (!loading && subChannel?.messagePreview) {
        renderResults(subChannel.messagePreview.messagePreviewId);
      }
    },
  );

  unsubscribe();
  ```

  ```swift iOS theme={null}
  let subChannelRepository = AmitySubChannelRepository()

  token = subChannelRepository.getSubChannel(withId: "sub-channel-id").observe { liveObject, error in
      if let error {
          handleError(error)
          return
      }

      guard let preview = liveObject.snapshot?.messagePreview else { return }
      showSuccessMessage(preview.messagePreviewId)
  }
  ```

  ```kotlin Android theme={null}
  val subChannelRepository = AmityChatClient.newSubChannelRepository()

  val disposable = subChannelRepository
      .getSubChannel(subChannelId)
      .subscribe(
          { subChannel ->
              val preview = subChannel.getMessagePreview()
              showSuccessMessage(preview?.getMessagePreviewId() ?: "")
          },
          { error -> handleGeneralError(error) },
      )
  ```
</CodeGroup>

## Preview Fields

| Field              | Description                                                                                    |
| ------------------ | ---------------------------------------------------------------------------------------------- |
| `messagePreviewId` | ID of the message represented by the preview.                                                  |
| `dataType`         | Message data type, such as text, image, file, video, audio, or custom.                         |
| `data`             | Preview payload for the message data type. For text messages, this contains the text payload.  |
| `channelId`        | Channel that owns the previewed message.                                                       |
| `subChannelId`     | Subchannel that owns the previewed message.                                                    |
| `subChannelName`   | Subchannel display name where the platform exposes it.                                         |
| `isDeleted`        | Whether the previewed message is deleted. Availability depends on the network preview setting. |
| `user`             | User object for the previewed message creator when the SDK has the user data.                  |

## Related Topics

<CardGroup cols={3}>
  <Card title="Query Channels" href="/social-plus-sdk/chat/conversation-management/channels/query-channels" icon="list">
    Load channel lists that include preview data.
  </Card>

  <Card title="Query Messages" href="/social-plus-sdk/chat/messaging-features/messages/query-and-filter-messages" icon="message">
    Load full message timelines when preview data is not enough.
  </Card>

  <Card title="Channel Unread Count" href="/social-plus-sdk/chat/engagement-features/unread-status/channel-unread-count" icon="hashtag">
    Combine previews with unread count and mention indicators.
  </Card>
</CardGroup>
