Key Benefit: Powerful message retrieval system that supports filtering by tags, message types, deletion status, and threading with real-time live collections for responsive chat interfaces.
Feature Overview
ThegetMessages
function provides comprehensive message retrieval capabilities, allowing you to fetch messages from specific subchannels based on various criteria. This function returns a live collection that automatically updates when messages are added, modified, or deleted.
Advanced Filtering
Comprehensive query parameters
- Filter by tags (including/excluding)
- Message type filtering
- Deleted message inclusion
- Thread-based querying
Real-time Updates
Live collection benefits
- Automatic data synchronization
- Real-time message updates
- Efficient pagination handling
- Memory-optimized collections
Implementation Guide
Retrieve messages from a subchannel with optional filteringQuery messages with various filtering options to get exactly the messages you need for your chat interface.
Required Parameters
Parameter | Type | Description |
---|---|---|
subChannelId | String | The ID of the subchannel from which to retrieve messages |
Optional Parameters
Parameter | Type | Description |
---|---|---|
includingTags | Array<String> | Filter messages that contain at least one of the specified tags |
excludingTags | Array<String> | Exclude messages that contain any of the specified tags |
includeDeleted | Boolean | Whether to include deleted messages in results (default: false) |
type | MessageType | Filter by specific message type (TEXT, IMAGE, FILE, AUDIO, VIDEO, CUSTOM) |
sortOption | SortOption | Sort order: firstCreated (oldest first) or lastCreated (newest first) |
Code Examples
Sorting Options: Use
firstCreated
for chronological order (oldest first) or lastCreated
for reverse chronological order (newest first, typical for chat interfaces).Query Strategies
Tag-Based Filtering
Tag-Based Filtering
Filter messages using custom tags for categorizationUse
includingTags
and excludingTags
to create sophisticated filtering systems:- Including Tags: Messages must have at least one of the specified tags
- Excluding Tags: Messages with any of these tags will be filtered out
- Combine Both: Create precise filters by including desired tags while excluding unwanted ones
Message Type Filtering
Message Type Filtering
Query specific types of messages for specialized viewsFilter by message type to create focused interfaces:
- Text Messages:
AmityMessage.DataType.TEXT
for text-only conversations - Media Messages:
IMAGE
,VIDEO
,AUDIO
for media galleries - File Messages:
FILE
for document sharing views - Custom Messages:
CUSTOM
for app-specific message types
Thread Management
Thread Management
Handle threaded conversations and reply structuresImplement sophisticated conversation threading:
- Top-Level Messages: Use
messageParentFilter: nil
orparentId: null
for main conversation flow - Thread Replies: Use
parentId
to get all replies to a specific message - Thread Navigation: Combine with sorting to maintain chronological order within threads
Message Navigation
Message Navigation
Best Practices
Efficient Pagination
Efficient Pagination
Optimize data loading and memory usage
- Use appropriate page sizes (15-20 messages for mobile, ~40 for desktop)
- Implement scroll-based pagination with threshold detection
- Cache previous pages to enable smooth bidirectional scrolling
- Monitor memory usage and release old pages when not needed
Real-time Synchronization
Real-time Synchronization
Maintain data consistency with live collections
- Always use live collections for active chat interfaces
- Handle connection state changes gracefully
- Implement proper cleanup when components unmount
- Use appropriate error handling for network issues
Performance Optimization
Performance Optimization
Optimize query performance and user experience
- Combine filters efficiently to reduce server load
- Use specific message type filters when building specialized views
- Implement proper loading states for better user experience
- Cache frequently accessed message collections
Related Features
Send Messages
Message Creation
Learn how to send new messages to channels
Edit & Delete
Message Management
Modify and remove messages after sending
Live Collections
Real-time Data
Understand live collection patterns and benefits
Implementation Strategy: Start with basic message querying using live collections, then add filtering capabilities as needed. Always implement proper error handling for network issues and invalid parameters to ensure robust chat experiences.