Introduction
Auto Refresh offers a way to re-render views of your application when the underlying data changes. This is useful when you want your views to always reflect the live database state. Auto Refresh can be an easy replacement for manually polling for changes using AJAX.Use Cases
Deployment Status
Used in Shipnix to display the current deployment status. Whenever the deployment progress or status changes, the view gets updated automatically.
Job Queue Monitoring
Building a monitoring tool for background job workers. Using auto refresh, the view can always represent the current state of all the job queues.
Setup
Auto Refresh requires no global configuration. Just make sure these two components are in your layout:1. Add Meta Tag to Layout
In yourWeb/View/Layout.hs, add {autoRefreshMeta} inside the <head> section:
2. Include Required JavaScript
In yourWeb/View/Layout.hs, ensure these scripts are included (order matters - morphdom must come before ihp-auto-refresh):
Once these two components are in place, you can use
autoRefresh in your actions — no middleware setup needed. The auto-refresh server is created lazily on first use.How It Works
It’s good to have a general understanding of how IHP Auto Refresh works.Activation
Auto Refresh is activated for an action by calling
autoRefresh. The framework automatically tracks all tables your action is using (e.g., in SELECT * FROM ... queries).Watching for Changes
Once the action sends a response, IHP starts watching for any
INSERT, UPDATE, or DELETE statement to all the tables used by your action.WebSocket Connection
When the page is rendered, a small JavaScript function connects back to the IHP server using a WebSocket connection.
Re-rendering
Whenever an
INSERT, UPDATE, or DELETE happens to the watched tables, IHP reruns your action on the server-side. When the generated HTML looks different than the HTML generated on the initial page load, it sends the new HTML to the browser using the WebSocket connection.Using Auto Refresh
Let’s say we have aShowProjectAction like this:
autoRefresh in front of the do:
That’s it! When you open your browser dev tools, you will see that a WebSocket connection has been started when opening the page. When you update the project from a different browser tab, the page instantly updates to reflect your changes.
Advanced Auto Refresh
Auto Refresh Only for Specific Tables
By default IHP tracks all the tables in an action with Auto Refresh enabled. In scenarios where you’re processing a lot of data for a view, but only a small portion needs Auto Refresh, you can enable Auto Refresh only for the specific tables:Custom SQL Queries with Auto Refresh
Auto Refresh automatically tracks all tables your action is using by hooking itself into the Query Builder andfetch functions.
Let’s say we’re using a custom SQL query:
sqlQuery, Auto Refresh is not aware that we’re reading from the companies table. In this case we need to help out Auto Refresh by calling trackTableRead:
trackTableRead marks the table as accessed for Auto Refresh and leads to the table being watched.