# Observe VPN Related Events

The **VPN SDK** on both mobile platforms provides an observer interface that can be implemented in order to handle any events that occur from the client.\
\
The **VPN SDK** for macOS and Windows provides a unified connection status event, offering real-time updates.

{% tabs %}
{% tab title="iOS" %}
The supported events that can be implemented are:

**Status/Configuration Events**

| Property           | Description                                                              |
| ------------------ | ------------------------------------------------------------------------ |
| serverWillChange   | The server defined in the client's `configuration` is about to change.   |
| serverDidChange    | The server defined in the client's `configuration` did change.           |
| protocolWillChange | The protocol defined in the client's `configuration` is about to change. |
| protocolDidChange  | The protocol defined in the client's `configuration` did change.         |

**Connection Events**

| Property                 | Description                                                       |
| ------------------------ | ----------------------------------------------------------------- |
| connectionWillBegin      | The connection to the VPN server is about to start connecting.    |
| connectionDidBegin       | The connection to the VPN server did start connecting.            |
| connectionWillReconnect  | The connection to the VPN server is about to attempt to reconnect |
| connectionSucceeded      | The connection to the VPN server was successful.                  |
| connectionFailed         | The connection to the VPN server failed with `error`.             |
| connectionWillDisconnect | The connection to the VPN server is about to disconnect.          |
| connectionDidDisconnect  | The connection to the VPN server did disconnect.                  |

**Miscellaneous Events**

|                        |                                                                                                                                                                                              |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| profileFailedToInstall | <p>The iOS VPN profile failed to install and returned with <code>error</code>.</p><p>Called on <code>connect(withConfiguration:completion:)</code> if the VPN pop-up fails or is denied.</p> |
| {% endtab %}           |                                                                                                                                                                                              |

{% tab title="Android" %}
To subscribe to observing connection state events, use the `subscribeToConnectionState` method. An identifier of the subscription and an instance of a class that implements the `ConnectionStateSubscriber` interface must be passed in. This class will be notified of changes to the connection state when the connection traverses between `DISCONNECTED`, `CONNECTING`, `CONNECTED` and `ERROR` states.

#### **Setting Up A Subscription** <a href="#setting-up-a-subscription" id="setting-up-a-subscription"></a>

To setup the subscription, call the `subscribeToConnectionState` method as so:

```kotlin
val subscriptionId = UUID.randomUUID().toString()
val subscriber = object : ConnectionStateSubscriber {
    override fun onStateChanged(newState: SudoVPNState) {
        when (newState) {
            SudoVPNState.CONNECTED -> {
                // Handle change in connection state
            }
            SudoVPNState.CONNECTING -> {
                // Handle change in connection state
            }
            SudoVPNState.DISCONNECTED -> {
                // Handle change in connection state
            }
            SudoVPNState.ERROR -> {
                // Handle change in connection state
            }
        }
    }
}
launch {
    try {
        sudoVPNClient.subscribeToConnectionState(
            subscriptionId,
            subscriber
        )
    } catch (e: SudoVPNException) {
        // Handle/notify the user of exception
    }
}
```

#### Cancelling A Subscription

To cancel a subscription, use the `unsubscribeFromConnectionState` or `unsubscribeAll` method. For example:

```kotlin
val subscriptionId = // From where the subscription was set up
launch {
    try {
        sudoVPNClient.unsubscribeFromConnectionState(subscriptionId)
        // or
        sudoVPNClient.unsubscribeAll()
    } catch (e: SudoVPNException) {
        // Handle/notify the user of exception
    }
}
```

This will ensure that the subscription is cancelled and system resources are freed.
{% endtab %}

{% tab title="Typescript" %}
To subscribe to observing connection state events, use the `subscribe` method. An identifier of the subscription and a function that accepts the `SudoVPNConnectionStatus` as a parameter must be passed in. This function will be notified of changes to the connection state when the connection traverses between states.

Valid States:\
`disconnected` \
`disconnecting` \
`connecting` \
`connected` \
`error` \
`unknown`&#x20;

#### **Setting Up A Subscription** <a href="#setting-up-a-subscription" id="setting-up-a-subscription"></a>

To setup the subscription, call the `subscribe` method as so:

```typescript
const subscriptionId = uuid() //unique id to identify the subscription
const subscriber: (status: SudoVPNConnectionStatus) => void = (status) => {
    // ...
}

const subscribeResult = await sudoVpnClient.subscribe(subscriptionId, subscriber)
```

#### Cancelling A Subscription

To cancel a subscription, use the `unsubscribe` or `unsubscribeAll` method. For example:

```typescript
const subscriptionId = // From where the subscription was set up

sudoVpnClient.unsubscribe(subscriptionId)
// or
sudoVpnClient.unsubscribeAll()    
```

This will ensure that the subscription is cancelled and system resources are freed.
{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sudoplatform.com/guides/virtual-private-network/observe-vpn-related-events.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
