> For the complete documentation index, see [llms.txt](https://doc.batch.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.batch.com/developer/sdk/web/listening-to-sdk-events.md).

# Listening to SDK events

You can subscribe to events broadcasted by the SDK when certain things happen. Doing so enables you to react to some events without having to continuously poll.

To do so, call `on()` to add an event listener. The arguments are:

* The event name
* A callback. The first argument will always be the api, other arguments depend on the event you're listening to.

```javascript
batchSDK(api => {
  api.on("event_name", (api, parameter) => {
    // Do something with the parameter.
  })
})
```

This documentation covers the most commonly used events. The full list of available events [can be found here](https://doc.batch.com/web-api-reference/enums/BatchSDK.SDKEvent.html#SubscriptionChanged).

### Broadcasted events

#### Subscription Changed

The `subscriptionChanged` event is fired whenever the user push subscription changes. It gives a single parameter, which is the new subscription.

For example, you can do something when the user accepted the notification permission and subscription succeeded:

```js
batchSDK(api => {
  api.on("subscriptionChanged", (api, subscription) => {
    if (subscription.subscribed) {
      // The user has subscribed to notifications
      // ...
    }
  })
})
```

{% hint style="info" %}
This event can be fired even without user action. Do not make the assumption that it will only be fired once: you may want to add a way to execute the callback only once using localstorage.
{% endhint %}

#### UI Ready

The `uiReady` event is fired whenever Batch is ready to display UI.

For example, you can do something when Batch is ready and fully initialized to display your custom UI.

```js
batchSDK(api => {
  api.on("uiReady", (api) => {
    // Show our hidden optin prompt
    // document.querySelector("#my-push-optin-prompt").style.display = "initial";
  })
})
```


---

# 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://doc.batch.com/developer/sdk/web/listening-to-sdk-events.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.
