> For the complete documentation index, see [llms.txt](https://pwa.spomky-labs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://pwa.spomky-labs.com/symfony-ux/sync-broadcast.md).

# 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`:

{% code title="assets/controllers.json" lineNumbers="true" %}

```json
{
    "controllers": {
        "@spomky-labs/pwa-bundle": {
            "backgroundsync-queue": {
                "enabled": true
            }
        }
    }
}
```

{% endcode %}

## Usage

{% code title="templates/components/sync-status.html.twig" lineNumbers="true" %}

```twig
<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>
```

{% endcode %}

## Parameters

| Parameter | Type   | Required | Description                                                                                                             |
| --------- | ------ | -------- | ----------------------------------------------------------------------------------------------------------------------- |
| `channel` | String | Yes      | The BroadcastChannel name to listen on. Must match the `broadcast_channel` configured in your background sync strategy. |

## Actions

| Action   | Description                                                                      |
| -------- | -------------------------------------------------------------------------------- |
| `replay` | Requests a replay/resync of all pending operations in the background sync queue. |

## Targets

| Target   | Description                                  |
| -------- | -------------------------------------------- |
| `output` | Element to display queue status information. |

## Events

| Event    | Detail                          | Description                                             |
| -------- | ------------------------------- | ------------------------------------------------------- |
| `error`  | `{ reason }`                    | BroadcastChannel initialization failed.                 |
| `status` | Status data from service worker | Status message received from the background sync queue. |

## How It Works

1. The controller opens a `BroadcastChannel` with the specified name
2. It requests the current sync queue status
3. When the service worker processes queued requests, it broadcasts status updates
4. The controller dispatches `status` events that you can listen for to update the UI
5. The `replay` action tells the service worker to retry all pending requests

## Example: Showing Pending Request Count

{% code title="templates/components/sync-indicator.html.twig" lineNumbers="true" %}

```twig
<div {{ stimulus_controller('pwa/backgroundsync-queue', {
    channel: 'form-sync'
}) }}
    data-action="pwa--backgroundsync-queue:status->updateStatus"
>
    <span class="badge" data-sync-count></span>
    <button {{ stimulus_action('pwa/backgroundsync-queue', 'replay', 'click') }}>
        Sync now
    </button>
</div>

<script>
function updateStatus(event) {
    const badge = document.querySelector('[data-sync-count]');
    badge.textContent = event.detail.pendingCount ?? '0';
}
</script>
```

{% endcode %}

## Related Configuration

This controller works with the [Background Sync](/the-service-worker/workbox/background-sync.md) service worker feature. Make sure the `broadcast_channel` name in your service worker configuration matches the `channel` parameter:

{% code title="config/packages/pwa.yaml" lineNumbers="true" %}

```yaml
pwa:
    serviceworker:
        workbox:
            background_sync:
                - queue_name: 'api-queue'
                  match_callback: 'regex: /\/api\//'
                  method: 'POST'
                  broadcast_channel: 'form-sync'  # Must match controller's channel param
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://pwa.spomky-labs.com/symfony-ux/sync-broadcast.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
