Fetch Past Messages
Overview
Fetching past messages lets your application retrieve historical data from a topic, providing important context or enabling catch-up scenarios when you miss real-time messages.
This feature complements subscriptions by allowing you to query previous messages stored on the server.
Use fetching in scenarios such as:
- Initial data loading on app startup
- Analysis or auditing of past events
- Recovery after extended offline periods
How Fetching Past Messages Works
- Specify the topic: You query a valid topic name (including wildcards) to retrieve messages published previously on that topic or on topics that match the wildcard pattern.
- Define fetch criteria: Control which messages are returned by specifying filters such as start and end time ranges.
- No real-time delivery: Unlike subscriptions, fetch does not keep delivering new messages as they arrive; it strictly returns historical data up to the request time.
- Connection state behavior: If you attempt to fetch message history while the client is disconnected or in the process of reconnecting, the fetch method will return an empty array, indicating no messages could be retrieved at that time.
// Select a platform to see code
Message Retention and TTL
- Expired Messages: Once a message's TTL (Time-to-Live) expires, the message is permanently deleted from the system and cannot be recovered or fetched.
- Plan Impact: Be aware that fetching past messages is only possible within the retention period defined by your plan. Attempting to fetch messages older than the retention window will yield no results.
Error Handling
history($topic, $start, $end) throws an error when,
- Invalid topic names or reserved system topics are used.
- The end date-time is greater than the start date-time.
Handle errors gracefully by validating input parameters before fetching and implementing retries or fallback logic as appropriate.
By leveraging past message fetching, your application gains powerful capabilities for historical data access and completeness, enabling rich and resilient real-time experiences.
Join our Discord server, post your concern & someone from our team will help you out ✌️