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

The getMessages 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

ParameterTypeDescription
subChannelIdStringThe ID of the subchannel from which to retrieve messages

Optional Parameters

ParameterTypeDescription
includingTagsArray<String>Filter messages that contain at least one of the specified tags
excludingTagsArray<String>Exclude messages that contain any of the specified tags
includeDeletedBooleanWhether to include deleted messages in results (default: false)
typeMessageTypeFilter by specific message type (TEXT, IMAGE, FILE, AUDIO, VIDEO, CUSTOM)
sortOptionSortOptionSort order: firstCreated (oldest first) or lastCreated (newest first)

Code Examples

// Basic message query with tag filtering
let messageQueryOptions = AmityMessageQueryOptions(
    subChannelId: "subChannel-123", 
    includingTags: ["games"], 
    excludingTags: ["staff-only"], 
    sortOption: .lastCreated
)
let messagesCollection = messageRepository.getMessages(options: messageQueryOptions)

// Observe messages with real-time updates
token = messageRepository.getMessages(options: messageQueryOptions).observe({ collection, change, error in
    if let error {
        print("Error: \(error).")
        return
    }
    for message in collection.allObjects() {
        // Handle each message in the collection
    }
})
Sorting Options: Use firstCreated for chronological order (oldest first) or lastCreated for reverse chronological order (newest first, typical for chat interfaces).

Query Strategies

Best Practices

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.