Query Global Feed

Overview

The social.plus SDK provides two powerful approaches for retrieving global feed content: conventional chronological ordering and intelligent custom post ranking. This flexibility allows you to create the optimal content experience for your users based on your application’s specific needs.

Feed Query Methods

Conventional Global Feed

Simple chronological ordering with the most recent posts appearing first. Perfect for real-time content streams.

Custom Post Ranking

Intelligent content curation using engagement metrics, time decay, and update boosts for optimal user experience.

Conventional Global Feed Query

The getGlobalFeed method provides a straightforward way to query posts in chronological order, ensuring users see the most recent content first.
The system displays the 20 most recent posts per community in the global feed. Older posts may not be visible to optimize performance and relevance.

Implementation

import AmitySDK

class GlobalFeedManager {
    private let feedRepository: AmityFeedRepository
    private var feedCollection: AmityCollection<AmityPost>?
    private var feedToken: AmityNotificationToken?
    
    init() {
        self.feedRepository = AmityFeedRepository(client: AmityManager.shared.client!)
    }
    
    func queryGlobalFeed() {
        // Create global feed query
        let query = feedRepository.getGlobalFeed()
        
        // Observe feed changes
        feedToken = query.observe { [weak self] collection, _, error in
            if let error = error {
                print("Global feed error: \(error.localizedDescription)")
                return
            }
            
            self?.feedCollection = collection
            self?.handleFeedUpdate(posts: collection.allObjects())
        }
    }
    
    private func handleFeedUpdate(posts: [AmityPost]) {
        print("Global feed updated with \(posts.count) posts")
        // Update UI with new posts
    }
    
    deinit {
        feedToken?.invalidate()
    }
}

Custom Post Ranking Query

The getCustomRankingGlobalFeed method provides intelligent content curation using our advanced scoring algorithm. This approach considers engagement metrics, time decay, and content updates to surface the most relevant posts for each user.
Custom post ranking requires SDK version 5.10 or higher. Posts created with earlier versions will not participate in the ranking algorithm.

Implementation

import AmitySDK

class CustomRankingFeedManager {
    private let feedRepository: AmityFeedRepository
    private var feedCollection: AmityCollection<AmityPost>?
    private var feedToken: AmityNotificationToken?
    
    init() {
        self.feedRepository = AmityFeedRepository(client: AmityManager.shared.client!)
    }
    
    func queryCustomRankingFeed() {
        // Create custom ranking global feed query
        let query = feedRepository.getCustomRankingGlobalFeed()
        
        feedToken = query.observe { [weak self] collection, _, error in
            if let error = error {
                print("Custom ranking feed error: \(error.localizedDescription)")
                return
            }
            
            self?.feedCollection = collection
            self?.handleRankedFeedUpdate(posts: collection.allObjects())
        }
    }
    
    private func handleRankedFeedUpdate(posts: [AmityPost]) {
        print("Custom ranking feed updated with \(posts.count) ranked posts")
        // Posts are automatically sorted by engagement score
        // Display posts in the order received for optimal user experience
    }
    
    deinit {
        feedToken?.invalidate()
    }
}

Key Features & Capabilities

Performance Considerations

Pagination Best Practices

  • Use appropriate page sizes (20-50 posts)
  • Implement lazy loading for optimal performance
  • Cache results when possible
  • Handle pagination state properly

Real-time Updates

  • Subscribe to feed updates for live content
  • Handle network reconnection gracefully
  • Implement proper memory management
  • Dispose of subscriptions when done

Error Handling

func handleFeedError(_ error: Error) {
    if let amityError = error as? AmityError {
        switch amityError {
        case .networkError:
            showNetworkErrorMessage()
        case .permissionDenied:
            showPermissionErrorMessage()
        case .rateLimitExceeded:
            showRateLimitMessage()
        default:
            showGenericErrorMessage()
        }
    }
}

Best Practices

Query Optimization

  • Choose appropriate query type based on use case
  • Set reasonable page limits
  • Use custom ranking for engagement-focused feeds
  • Implement proper caching strategies

User Experience

  • Provide loading states during queries
  • Handle empty states gracefully
  • Implement pull-to-refresh functionality
  • Show appropriate error messages

JavaScript/TypeScript queryAllPosts Method

For JavaScript and TypeScript applications, you can use the queryAllPosts method with custom ranking enabled:
import { PostRepository } from '@amityco/js-sdk';

class GlobalFeedWithRanking {
    constructor() {
        this.postRepository = new PostRepository();
    }
    
    async queryGlobalFeedWithRanking() {
        try {
            const posts = await this.postRepository.queryAllPosts({
                customRanking: true,
                limit: 20,
                includeDeleted: false
            });
            
            this.handleRankedPosts(posts);
            return posts;
            
        } catch (error) {
            this.handleQueryError(error);
            throw error;
        }
    }
    
    handleRankedPosts(posts) {
        console.log(`Retrieved ${posts.length} ranked posts`);
        // Posts are sorted by engagement score
    }
    
    handleQueryError(error) {
        console.error('Query error:', error.message);
        
        // The queryAllPosts method will throw an error if parameters are incorrect
        if (error.message.includes('parameters')) {
            console.error('Please check your query parameters');
        }
    }
}
The queryAllPosts method will throw an error if incorrect parameters are passed. Always validate your parameters and handle errors appropriately.