# Connect to Ignite

When connecting to Ignite, ensure the Host App is in the foreground, active, and visible to the user. DT sends callbacks on the thread that the initialization was executed on.

There are no restrictions on the lifecycle owners for the connection: Activity, Fragment, Service, ViewModel, etc. However, do not connect to Ignite from classes or methods that might be called while the Host App is in the background, such as the Application class or broadcast receivers. Connecting in this manner causes multiple unnecessary connection attempts, potential resource conflicts, and unexpected behaviors when receiving broadcasts in the background.

After successful initialization and authentication, the SDK persists an authorization token and refreshes it as needed. The SDK uses the token for any requests that require authorization.

{% hint style="info" %}
Initialization failure indicates that Ignite is not present on the device.
{% endhint %}

To connect to Ignite via the SDK:

1. Declare and create an instance of `IIgniteService`:

{% code title="Kotlin" %}

```kotlin
lateinit var igniteService: IIgniteService
// ...

// After SDK is initialised the IgniteService instance is returned.
// It can also be retrieved later using IgniteServiceSdk.instance()
// NOTE: `clientSecret` can be empty for initial integration
igniteService = IgniteServiceSdk.init(context, clientSecret)
```

{% endcode %}

2. Call the `connect()` method:

{% code title="Kotlin" %}

```kotlin
IgniteServiceSdk.instance().connect(object: IConnectionCallback {
     override fun onConnected() {
          // Connection with Ignite is established
          Toast.makeText(applicationContext, "Connected.", Toast.LENGTH_SHORT).show()
     }

	 override fun onAuthenticated() {
	      // Client is now able to call API methods
          Toast.makeText(applicationContext, "Authenticated.", Toast.LENGTH_SHORT).show()
     }

     override fun onDisconnected(message: String?) {
          Toast.makeText(applicationContext, "Disconnected from Ignite Service: $message", Toast.LENGTH_SHORT).show()
     }
})

// or

igniteService.connect(this)
```

{% endcode %}

## Example <a href="#h_01k25vm0hnkr5vc01zx9jfshy6" id="h_01k25vm0hnkr5vc01zx9jfshy6"></a>

{% code title="Kotlin" %}

```kotlin
class ExampleActivity: AppCompatActivity(), IConnectionCallback {
    private lateinit var igniteService: IIgniteService

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        igniteService = IgniteServiceSdk.init(this, clientSecret = "")
        igniteService.connect(this)
    }

    override fun onConnected() {
        Log.i(TAG, "Connected to Ignite Service")
    }

    override fun onDisconnected(message: String?) {
        Log.i(TAG, "Disconnected from Ignite Service: $message")
    }

    override fun onAuthenticated() {
        // Toast cannot be called on other threads except the main one
        Handler(Looper.getMainLooper()).post {
            Toast.makeText(applicationContext, "Authenticated", Toast.LENGTH_SHORT).show()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        
        // Disconnect from the service if needed
        igniteService.disconnect(this)
    }
    
    companion object {
        const val TAG = "SDK"
    }
}
```

{% endcode %}

## Troubleshooting Connections <a href="#h_01k25vm0hpxf1y0hf9yr5c6fst" id="h_01k25vm0hpxf1y0hf9yr5c6fst"></a>

If your Host App is unable to connect to Ignite, you may receive one of the following error messages:

| ERROR MESSAGE                                    | DESCRIPTION                                                                                          |
| ------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
| Connection failed to unknown reason              | Ignite is not present in the device.                                                                 |
| Authentication Exception: Can not create session | `Client ID`, `Client Secret`, or package name does not match what was given for DT Configuration.    |
| Authentication Exception Code -2                 | Host App signing certificate fingerprint does not match with what was provided for DT Configuration. |
