# Optimal Location and Specific Locations

When implementing specific locations in your VPN service, you have two primary options for specifying the hop location: optimal location and specific location.

## Optimal Location

The optimal location feature allows the VPN SDK to automatically select the best VPN server for your connection. This is ideal when you want to prioritize performance and let the system handle the routing decisions.

To use the optimal location in your code, you can utilize the `VirtualLocation.optimal()` method. Here's an example of how to start a VPN connection with the optimal location:

```swift
import VPNApplicationSDK
// ...

let groupData = VPNGroupData(groupID: "group.com.yourcompany.vpnsdk-demo", usesSystemExtension: false)

let hydraConfiguration = HydraConfiguration(
    carrierID: "YOUR_CARRIER_ID",
    extensionBundleID: "com.yourcompany.vpnsdk-demo.neprovider",
    groupData: groupData,
    fireshieldConfig: FireshieldConfig(mode: .disabled, groupData: groupData)
)

let hydraSDK = HydraSDK(configuration: hydraConfiguration)

hydraSDK.start(location: VirtualLocation.optimal(), proxy: nil, completion: { error, credential in
    if let error = error {
        print("Failed \(error)")
    } else {
        print("Success")
    }
})
```

To display the optimal location (country and city) for a user in your app's interface, you can retrieve this information from the `credential` data returned by the Hydra SDK. Here's how to do it:

```swift
hydraSDK.start(location: VirtualLocation.optimal(), proxy: nil, completion: { error, credential in
    let optimalCountry = credential?.connectedServerCountry
    let optimalCity = credential?.connectedServerCity
})
```

{% hint style="info" %}
When configuring the location parameter, using `location: nil` has the same effect as `location: VirtualLocation.optimal()`. Both options enable the SDK to automatically select the most suitable proxy server. However, we recommend explicitly using `location: VirtualLocation.optimal()` as it provides better code clarity and adheres to best practices by making your intent more explicit.
{% endhint %}

Please note that the `connectedServerCountry` and `connectedServerCity` properties will not be available before the connection is established, as the VPN SDK does not know which VPN server the backend will connect to as optimal. These properties can only be accessed after the connection has been made.

## Accessing Specific Locations

For users who require more control over their connection routing, the specific location option allows you to specify a particular intermediate server location.

Here's an example of finding a VPN connection with a custom location using `virtualLocations`:

```swift
let selectedCity = "San Jose"
hydraSDK.virtualLocations( completion: { (virtualLocationsError, result) in
    guard let virtualLocations = result else {
        print("No virtual locations available")
        return
    }

    var foundMatchingLocation = false
    for virtualLocation in virtualLocations {
        if virtualLocation.city == selectedCity {
            print("Found matching location: ", virtualLocation)
            foundMatchingLocation = true
        }
    }

    if !foundMatchingLocation {
        print("No matching location found")
    }
})
```

In this example, the intermediate hop is a server in San Jose, United States.

{% hint style="info" %}

* For more information, refer to [struct VirtualLocation](https://pango.gitbook.io/paas/sdk/api-reference#struct-virtuallocation).
* For comparison to Multihop, refer to [#multihop-vs-virtual-location-routing](https://pango.gitbook.io/paas/sdk/unified-vpn-sdk-for-apple/features/multihop/..#multihop-vs-virtual-location-routing "mention")
  {% endhint %}

## Optimal Location vs. Specific Locations

It is important to note that Optimal location and specific locations are two separate features that can be configured independently. They offer flexibility in managing VPN connections but serve different purposes.

Optimal location is a feature that automatically selects VPN nodes based on predefined rules. These rules are designed to provide the best possible connection based on factors such as geographic proximity, server load, and network performance. In some cases, users may not have control over the predefined logic used by the Optimal location feature.

Specific locations, on the other hand, refer to a list of locations that are made available to users. This list may not necessarily include all the VPN nodes that are available through the Optimal location feature. Specific locations allow users to manually select specific locations from the provided list.

Optimal location and specific locations are two powerful features in VPN configuration that provide flexibility and control over how users connect to VPN nodes. While Optimal location automatically selects the best node based on predefined rules, specific locations allow users to manually choose from a specific list of locations.

### Example Scenario

To better understand the difference between Optimal location and specific locations, consider the following example:

Suppose a VPN service provider wants to configure their system so that all users in the United States are automatically connected to VPN nodes located in the closest major cities with available data centers. These cities might include San Francisco, Boston, New York, Seattle, and others. This is where the Optimal location feature comes into play, automatically selecting the best node based on the user's location.

In addition to the Optimal location feature, the service provider wants to give each user the option to manually select from two specific specific locations: New York and Boston. In this case, the specific locations list will only contain these two locations, even though the Optimal location feature might connect users to nodes in other cities.

It's important to note that the Optimal location feature might return nodes that belong to a location not included in the specific locations list.
