Skip to main content
Live Data Objects: Message retrieval returns live objects that automatically update when message content changes, ensuring your UI always displays the most current information without manual refresh operations.

Message Retrieval Overview

Retrieve and display message content using live data objects that provide real-time updates and comprehensive content handling for all message types including text, images, videos, files, and custom messages.

Live Objects

Real-time updates
  • Automatic content synchronization
  • Live data object observation
  • Real-time change notifications
  • Seamless UI updates

All Content Types

Comprehensive support
  • Text message handling
  • Image and video processing
  • File attachment management
  • Custom message structures

Implementation Guide

  • Single Message Retrieval
  • Text Message Handling
  • Media Message Handling
  • Custom Message Handling
Retrieve individual messages by IDGet specific messages using their unique identifiers with live data observation for real-time updates.

Required Parameters

ParameterTypeDescription
messageIdStringUnique identifier of the message to retrieve

Return Value

TypeDescription
LiveObject<AmityMessage>Live data object with automatic updates

Code Examples

// Get single message with live updates
func observeMessage(messageId: String) {
    let messageObject = messageRepository.getMessage(messageId)
    
    // Observe message changes
    token = messageObject.observe { [weak self] messageObject, error in
        if let error = error {
            self?.handleError(error)
            return
        }
        
        guard let message = messageObject.object else {
            print("Message not found or deleted")
            return
        }
        
        // Update UI with message content
        self?.displayMessage(message)
        
        // Handle different message types
        self?.processMessageContent(message)
    }
}
Live Data Benefits: Live objects automatically update when message content changes, eliminating the need for manual refresh calls and ensuring your UI always displays current information.

Message State Management

Managing real-time message updates and synchronizationObserver Pattern Benefits
  • Automatic Updates: Messages update automatically when content changes
  • Real-Time Sync: Changes from other users appear instantly
  • Error Handling: Built-in error handling for network and data issues
Implementation Best Practices
  • Lifecycle Management: Properly dispose of observers to prevent memory leaks
  • Error Recovery: Implement retry logic for failed observations
  • Loading States: Show appropriate loading indicators during data fetching
Performance Optimization
  • Selective Observation: Only observe messages currently visible to users
  • Debounced Updates: Prevent excessive UI updates with debouncing
  • Background Processing: Handle heavy data processing off the main thread
Robust error handling for message retrieval and displayError Types and Handling
  • Network Errors: Connection timeouts, network unavailability
  • Permission Errors: Insufficient access rights to view messages
  • Content Errors: Corrupted or invalid message data
  • Rate Limiting: API rate limit exceeded errors
Recovery Strategies
  • Automatic Retry: Retry failed requests with exponential backoff
  • Fallback Content: Show cached or placeholder content when fresh data fails
  • User Feedback: Clear error messages and recovery options for users
  • Graceful Degradation: Maintain basic functionality even with partial failures
User Experience
  • Loading States: Show appropriate loading indicators during operations
  • Error Messages: User-friendly error messages with actionable solutions
  • Offline Support: Enable offline viewing of cached messages
  • Retry Options: Allow users to manually retry failed operations
Implementation Priority: Start with basic text and image message display, then progressively add support for other content types. Always implement proper error handling and loading states for the best user experience.