# Sessions

> When the SDK starts and ends a session (separate foreground and background timeouts, continued across restarts), what it records for each one, and what a new session triggers.

A session is one continuous period of use of your app, from the first activity until 30 minutes pass without any. The SDK tracks sessions on its own; you do not call anything to start or end them.

## How sessions behave

| Rule | Behavior |
|---|---|
| Start | The first SDK activity on a device, or the first activity after the timeout has passed |
| Timeout | 30 minutes without activity in the foreground, or 30 minutes in the background (each configurable) |
| Session ID | The start time in epoch milliseconds, sent as `sessionId` on every event |
| `session_start` event | Queued when the session starts, with `session_index` (sessions on this install, from 1) and `previous_session_id` (null for the first). SDK 0.6.0 and later. |
| `session_end` event | Queued when the next session starts, with `durationSeconds` (last activity minus start) and a timestamp at the last activity |
| On start | The SDK fetches the user's content cards and in-app messages, then evaluates in-app messages with a session start trigger |

Activity means any recorded SDK call (an event, an attribute, an interaction) and the app coming to the foreground. When the app goes to the background, the SDK saves the time as the last activity and flushes the queue.

## Sign-out and user switch

`reset()` and identifying a different user both end the current session and start a new one. The `session_end` is still attributed to the previous user. The same rules apply on iOS and Android.

The foreground timeout counts from the last activity while the app is in the foreground: a foreground app left idle for 30 minutes starts a new session at its next activity. Once the app goes to the background, the background timeout counts instead, from the moment it went there: coming back after it starts a new session.

A cold start within the foreground timeout of the last activity continues the same session, with the same session ID (when the app was last in the background, for example killed there, the background timeout decides instead); the SDK keeps the session ID, its last activity and the session index on the device. `session_index` counts from SDK 0.6.0: an app updated from an earlier SDK starts at 1.

## Change the timeout

:::tabs
::tab ios
```swift
Herald.configure(appKey: "YOUR_APP_KEY", endpoint: URL(string: "https://YOUR_HERALD_HOST")!) { options in
    options.foregroundSessionTimeout = 15 * 60 // seconds
    options.backgroundSessionTimeout = 5 * 60
}
```
::tab android
```kotlin
Herald.configure(
    this,
    appKey = "YOUR_APP_KEY",
    endpoint = "https://YOUR_HERALD_HOST",
    options = HeraldOptions(foregroundSessionTimeoutMinutes = 15, backgroundSessionTimeoutMinutes = 5),
)
```
:::

The older single setting (`sessionTimeout` on iOS, `sessionTimeoutMinutes` on Android) still works and sets both; it is deprecated.

## Edge cases

- **The app is killed.** The last activity is saved to disk whenever the app moves to the background, and at least once a minute while it runs. The `session_end` for that session is sent when the next one starts, with the duration up to the last saved activity.
- **A session starts in the background,** for example when a notification tap records an event before the app is visible. The session start trigger for in-app messages waits until the app is in the foreground.
- **The SDK is disabled** with `setEnabled(false)`. No sessions start and no events are recorded until it is enabled again.

## Related

- [Events and attributes](/docs/concepts/events-and-attributes)
- [In-app message triggers](/docs/in-app-messages/triggers-and-delivery)
- [Data batching and offline behavior](/docs/concepts/batching-and-offline)
