Polling
Automatically refresh components at specified intervals with intelligent request batching and viewport detection.
Basic usage
Add lace:poll.5s="method" to any element and Interlace calls that method every 5 seconds,
then re-renders. Here refresh just stamps the current time:
Last updated
11:00:57
Polled 0 times, every 5 seconds
from datetime import datetime
from interlace import Component
class PollClock(Component):
ticks: int = 0
updated_at: str = ""
def mount(self):
self.updated_at = datetime.now().strftime("%H:%M:%S")
def refresh(self): # called every tick
self.ticks += 1
self.updated_at = datetime.now().strftime("%H:%M:%S")
def inline_template(self):
return """
<div lace:poll.5s="refresh">
<p>Last updated</p>
<p>{{ updated_at }}</p>
<p>Polled {{ ticks }} times, every 5 seconds</p>
</div>
"""
Re-render without a method
Drop the method and a poll simply re-renders the component each tick - for when the freshness comes from the data itself (a query, the clock, a cache value), not from changing state. Nothing to define:
<div lace:poll.2s>
{{ unread_count }} unread
</div>
That's the built-in $refresh action - a "re-render me" you can also trigger
directly, from a button or Alpine:
<button lace:click="$refresh">Refresh</button>
// from Alpine
$lace.$refresh()
Where the fresh data comes from. A bare re-render reflects values read at render time - your template, a
@property, or@computed. It does not re-runmount()(which runs once), so a value you fetched into a property inmountwon't update on its own. Read it at render time, or uselace:poll.2s="reload"with a method that re-queries into the property.
Need to run code on each tick (refetch into a property, mutate state)? Give the poll a
method: lace:poll.2s="reload".
Start and stop polling
A poll only runs while its element is on the page, so wrapping lace:poll in a condition
starts and stops it. Tick the checkbox to render the polling element, untick it to drop it:
class PollToggle(Component):
polling: bool = False
updated_at: str = ""
def updated_polling(self, value): # fires when the checkbox flips
if value:
self.refresh()
def refresh(self):
self.updated_at = datetime.now().strftime("%H:%M:%S")
def inline_template(self):
return """
<input type="checkbox" lace:model.live="polling"> Poll every 2 seconds
{% if polling %}
{# rendered only while checked -> polls; unchecked -> the poll is gone #}
<div lace:poll.2s="refresh">Last updated {{ updated_at }}</div>
{% else %}
<div>Polling stopped</div>
{% endif %}
"""
Automatic request batching
A real page rarely polls just once. A dashboard might refresh stats, an activity feed, and a notification badge - three polling elements, maybe in three separate components. Naively that's three HTTP requests on every tick, each with its own snapshot payload and round-trip.
Interlace collapses them. Every poll due in the same window is bundled into one
request to /interlace/message - across all components on the page - and the
server renders them together and returns one response that morphs each in place. Three widgets
polling every 2 seconds cost you one request every 2 seconds, not three.
<!-- Three components, three updates - one HTTP request every 2 seconds -->
<div lace:poll.2s="update_stats">{{ stats }}</div>
<div lace:poll.2s="update_feed">{{ feed }}</div>
<div lace:poll.2s="update_badge">{{ badge }}</div>
Different intervals coalesce too
The batching isn't limited to polls that share an interval. Every poll on the page is
scheduled against a single shared clock, so a .1s, a
.2s, and a .4s poll all tick on the same boundaries - and
whenever those boundaries line up, the polls ride in one request. A 50ms tolerance
window absorbs scheduling jitter so near-simultaneous ticks still merge.
| time → | 1s | 2s | 3s | 4s | 5s | 6s | 7s | 8s |
|---|---|---|---|---|---|---|---|---|
| poll.1s | ● | ● | ● | ● | ● | ● | ● | ● |
| poll.2s | · | ● | · | ● | · | ● | · | ● |
| poll.4s | · | · | · | ● | · | · | · | ● |
| requests | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| updates | 1 | 2 | 1 | 3 | 1 | 2 | 1 | 3 |
Over 8 seconds those three polls schedule 14 ticks - but the page sends only 8 requests, one per second. At the 4s and 8s marks all three intervals align and a single request carries three component updates at once.
Polling pauses entirely while the tab is hidden and resumes with a single refresh - see catch-up behavior.
Poll only when visible - .visible
Add .visible and a poll runs only while its element is in the viewport. Scroll it off
screen and the ticks pause; scroll it back and they resume. Ideal for a feed, chart, or
badge below the fold - you stop spending requests on data nobody is looking at.
<div lace:poll.5s.visible="refresh_feed">
{% for post in feed %}
<p>{{ post.title }}</p>
{% endfor %}
</div>
Keep polling in the background - .keep-alive
By default a poll pauses while the browser tab is hidden and does one catch-up refresh
when you come back. For a poll that must keep running even in a background tab - a live
price, a countdown, a job you are waiting on - add .keep-alive.
<div lace:poll.1s.keep-alive="check_price">
{{ symbol }}: {{ price }}
</div>
Reach for it sparingly: a background tab that keeps hitting the server is exactly what the default pause is there to prevent.
Replay missed ticks - .replay
While a poll is paused (tab hidden, or scrolled away under .visible), Interlace
coalesces the ticks it missed: on return it fires once for the latest state, so you
never get a burst. That is the right default for a plain refresh.
Some polls are cumulative, where every tick matters - stepping an animation, draining a
queue one item per tick. Add .replay and each missed tick is replayed instead of
collapsed into one.
<div lace:poll.1s.replay="advance_frame">
Frame {{ frame }}
</div>
Reference
See lace:poll for the full directive syntax, every modifier, and the
catch-up behavior.