Sync Broadcast
The Sync Broadcast controller (backgroundsync-queue) monitors background sync queue status using the BroadcastChannel API. It allows your application to display real-time information about pending sync operations and trigger replays.
Installation
Enable the controller in your assets/controllers.json:
{
"controllers": {
"@spomky-labs/pwa-bundle": {
"backgroundsync-queue": {
"enabled": true
}
}
}
}Usage
<div {{ stimulus_controller('pwa/backgroundsync-queue', {
channel: 'form-sync'
}) }}
{{ stimulus_action('pwa/backgroundsync-queue', 'replay', 'click') }}
>
<span {{ stimulus_target('pwa/backgroundsync-queue', 'output') }}>
Checking sync status...
</span>
<button type="button">Retry pending items</button>
</div>Parameters
channel
String
Yes
The BroadcastChannel name to listen on. Must match the broadcast_channel configured in your background sync strategy.
Actions
replay
Requests a replay/resync of all pending operations in the background sync queue.
Targets
output
Element to display queue status information.
Events
error
{ reason }
BroadcastChannel initialization failed.
status
Status data from service worker
Status message received from the background sync queue.
How It Works
The controller opens a
BroadcastChannelwith the specified nameIt requests the current sync queue status
When the service worker processes queued requests, it broadcasts status updates
The controller dispatches
statusevents that you can listen for to update the UIThe
replayaction tells the service worker to retry all pending requests
Example: Showing Pending Request Count
Related Configuration
This controller works with the Background Sync service worker feature. Make sure the broadcast_channel name in your service worker configuration matches the channel parameter:
Last updated
Was this helpful?