Polling With Trongate MX
Polling is a powerful feature in Trongate MX that allows you to periodically fetch updates from the server without user interaction. This can be particularly useful for real-time updates, live data feeds, or any scenario where you need to keep your web application's content fresh and up-to-date.
Usage
Polling in Trongate MX is implemented using the mx-trigger
attribute. By specifying a polling interval, you can instruct Trongate MX to automatically send requests to the server at regular intervals.
Basic Example:
<div mx-get="api/live_data" mx-trigger="every 5s">
<!-- Content will be updated every 5 seconds -->
</div>
In this example, the content of the <div>
will be updated with data from the specified URL every 5 seconds.
Polling Options
Trongate MX supports several polling options to provide flexibility in how and when updates are fetched:
Option | Description | Example Syntax |
---|---|---|
Basic Polling | Polls at a regular interval starting immediately. | mx-trigger="every 10s" |
Load Polling | Starts polling after an initial delay. | mx-trigger="load delay:3s" |
Polling with Initial Delay | Combines an initial delay with a different interval for subsequent requests. | mx-trigger="load delay:2s, every 7s" |
Time Interval Specification
Trongate MX allows you to specify time intervals using different units:
s
: secondsm
: minutesh
: hoursd
: days
This flexibility allows you to set up polling intervals that best suit your application's needs.
Examples:
Basic Polling (every X seconds):
The code sample below fetches updates from the server every 3 seconds, providing real-time data without page refresh:
<div mx-get="api/updates" mx-trigger="every 3s"></div>
Load Polling (delay then poll every X minutes):
This code initiates polling after a 5-minute delay, then continues to poll every 5 minutes, useful for less frequent updates:
<div mx-get="api/data" mx-trigger="load delay:5m"></div>
Polling with Initial Delay:
This example waits for 1 hour before the first poll, then polls every 30 minutes thereafter, ideal for long-term data tracking:
<div mx-get="api/stats" mx-trigger="load delay:1h, every 30m"></div>
- Consider server load: Be mindful of the polling frequency to avoid overwhelming your server with requests.
- Use appropriate intervals: Choose polling intervals that make sense for your data's update frequency.
- Combine with other attributes: Use polling with
mx-target
andmx-swap
for more dynamic updates. - Error handling: Implement proper error handling to manage failed requests gracefully.
Additional Notes
- Polling is only applied to elements that exist when the page is initially loaded.
- Polling works with various request types but specifically excludes
mx-post
requests to prevent unintended repeated form submissions. - The polling mechanism is handled entirely client-side, making it easy to implement without server-side changes.
By leveraging polling in Trongate MX, you can create dynamic, real-time experiences for your users without the need for complex JavaScript or WebSocket implementations. This feature allows your web applications to stay current and responsive with minimal effort.