Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 28 ยท Projects

Project: Dashboard with Polling, Tabs and Toasts

Build a dashboard with HTMX and Alpine.js: polling widgets with hx-trigger every, lazy-loaded tab panels, and toast notifications driven by HX-Trigger events.

Dashboards are where HTMX's polling and event features shine. Numbers refresh on their own, panels load only when someone looks at them, and actions report back with small notifications. The server still renders all the HTML; the browser only needs a sprinkle of Alpine for tabs and toasts.

This project has three parts: polling widgets that update themselves, tabs whose content loads lazily the first time they are opened, and toasts, small messages that appear when the server sends an HX-Trigger response header. If you need a refresher, see Common Interaction Patterns for polling and lazy loading, and Events, Headers and JavaScript Integration for HX-Trigger.

Three techniques

Polling hx-trigger="every 2s" refreshes a widget.
Lazy panels Content loads the first time a tab opens.
Toasts The server announces events in a header.
1 / 2

Step 1: polling widgets

Add every 2s to hx-trigger and HTMX sends the request again and again. Combine it with load (hx-trigger="load, every 2s") so the widget fills immediately instead of after the first interval.

Polling has costs: every open dashboard sends requests all day. Good habits are:

  • Pause when it makes sense. A trigger filter in brackets runs before every poll. Here, [!window.paused] skips the request while a global flag is set. Alpine owns the flag, because "paused" is view state.
  • Use sensible intervals. Two to five seconds suits a live-looking counter; slower for anything that changes rarely.
  • Let the server stop it. A response with HTTP status 286 tells HTMX to stop polling that element. This is useful for jobs that finish ("export complete").
Polling widgets with a pause button

Watch the update numbers climb, then pause: the numbers freeze, and no requests leave the page. (Open your browser's network panel on a real site to see the same thing.) Each widget polls on its own schedule, and each request returns only the small inner HTML.

Polling inside an element that gets swapped is safe: when HTMX replaces the element, the old timer disappears with it. If a widget is outerHTML-swapped, make sure the new HTML still carries the hx-trigger attribute, or polling stops after the first reply.

Step 2: tabs, lazy panels and toasts

Now the full dashboard.

Tabs are view state. Alpine holds tab and shows one panel at a time with x-show. No request is needed to switch.

Lazy loading. The Orders and Reports panels should load only if someone opens them. Each tab button dispatches a custom event with $dispatch('tab-orders'), and the panel listens for it with hx-trigger="tab-orders from:body once". The once modifier means it loads on the first visit and never again.

Toasts through HX-Trigger. The Export report button sends a request with hx-swap="none". The server replies with the header:

TEXT
HX-Trigger: {"toast": {"message": "Report exported", "kind": "ok"}}

HTMX turns this into a DOM event named toast on the element that made the request, with the JSON object as event.detail. The event bubbles up, so an Alpine listener on the toast container, @toast.window="...", can catch it. The server never touches Alpine, and Alpine never parses a response: only a named event with data connects them.

Errors as toasts. The Restart worker button gets an HTTP 500. HTMX does not swap error responses, but it fires htmx:responseError, and another Alpine listener turns that into a red toast.

Dashboard: polling, tabs and toasts

Try it in this order: leave the Overview tab running and watch the numbers update. Open Orders: "Loading orders..." is replaced after one request. Switch away and back: the counter stays at "1 time(s)", because once prevents a second load. Press Export report for a green toast, then go back to Overview and press Restart worker for a red one. Click a toast to dismiss it, or wait a few seconds.

Design notes

  • Panels hidden with x-show are still in the DOM. That is why the Overview widgets keep polling while you look at another tab. If that bothers you, give the trigger a filter such as [window.activeTab === 'overview'], or use the pause button from step 1.
  • Name events after what happened. toast here, but order-created or cart-changed are just as common. Several parts of the page can then react to the same event without knowing about each other.
  • HX-Trigger has timing variants. HX-Trigger-After-Swap and HX-Trigger-After-Settle fire later in the response lifecycle, handy when the event handler needs the new content already in place.
  • Toasts need to be accessible. The container has aria-live="polite", so screen readers announce new messages without interrupting. Do not rely on color alone: the text says what happened.

The mock server returns headers through reply.headers. On a real server you set the same header in your framework, for example response()->header('HX-Trigger', json_encode([...])) in Laravel or response["HX-Trigger"] = json.dumps({...}) in Django.

Ideas to extend it

  1. Stop a poll from the server: return status 286 when a job is finished.
  2. Replace the toast timer with a progress bar using Alpine's x-transition.
  3. Poll a "last updated" label with hx-trigger="every 30s" and flash it using the htmx-settling class.
  4. Add HX-Trigger: order-created after a form post and have the Orders tab refresh itself with hx-trigger="order-created from:body".

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does hx-trigger="load, every 2s" do?

    load fires one request immediately when the element appears, and every 2s repeats it, so the widget never sits empty for the first interval.
  2. 2. What does the once modifier in hx-trigger="tab-orders from:body once" do?

    With once, HTMX removes the trigger after the first firing, so reopening the tab does not request the panel again.
  3. 3. How does a toast message get from the server into Alpine in this project?

    HX-Trigger carries an event name and JSON detail. HTMX dispatches it as a DOM event that bubbles, and an Alpine listener on window handles it.
  4. 4. A server answers an HTMX request with status 500. What happens by default?

    HTMX only swaps 2xx responses by default; for errors it fires htmx:responseError, which the dashboard turns into a red toast.
  5. 5. What does an HTTP 286 response do to a polling element?

    286 is a special status that HTMX treats as 'stop polling', which lets the server end a poll when a job finishes.

Summary

  • hx-trigger="load, every 2s" creates a self-refreshing widget; a bracket filter such as [!window.paused] can skip polls, and a 286 status stops them.
  • Tabs are Alpine view state; $dispatch plus hx-trigger="event from:body once" loads each panel lazily, one time.
  • The server can announce events with the HX-Trigger header; Alpine listens with @name.window.
  • Error responses are not swapped but fire htmx:responseError, which is a good hook for an error toast.
  • Use aria-live for notifications and keep polling intervals reasonable.

Continue with Exercises with Solutions.