AnyBio
Guides

Session Management

BioSDK automatically manages sessions with the AnyBio platform. A session is created when a device connects and streaming begins, and ended when streaming stops or the device disconnects.

Session Lifecycle

Sessions are managed automatically — the SDK starts a session on the backend when streaming begins and ends it when streaming stops. You receive session events through the SDK's event publisher:

sdk.events.sink { event in
    if case .session(let sessionEvent) = event {
        switch sessionEvent {
        case .started(let id, let devices):
            print("Session started: \(id)")
        case .resumed(let id, let devices):
            print("Resumed existing session: \(id)")
        case .ended(let id, let ok):
            print("Session ended: \(id), success: \(ok)")
        case .conflict(let activeId, let startedAt):
            print("Conflict! Active session: \(activeId)")
        case .recoveryAvailable(let info):
            print("Orphaned session found: \(info.sessionId)")
        case .forbidden:
            print("Authorization failed")
        case .serverError(let status):
            print("Server error: \(status)")
        case .error(let message):
            print("Session error: \(message)")
        case .endFailed(let id, let status):
            print("Failed to end session \(id)")
        }
    }
}

Conflict Handling

If a previous session is still active on the backend (e.g., the app was force-quit without ending the session), the SDK emits a .conflict event. You can resolve it in two ways:

// Option 1: Resume the existing session
sdk.resumeConflictingSession()

// Option 2: End the old session and start a new one
sdk.endConflictingAndStartNew()

The conflicting session ID is available on the SDK state:

if let conflictId = sdk.state.conflictingActiveSessionId {
    // Present UI asking user to resume or start fresh
}

Session Mode

Session mode controls whether the SDK automatically creates backend sessions. Disable it if you want to manage session lifecycle externally:

// Disable automatic session management
sdk.setSessionMode(enabled: false)

// Re-enable
sdk.setSessionMode(enabled: true)

Session Context

The SDK maintains a published session context that you can observe in SwiftUI:

// Observable session state
sdk.sessionContext  // BioSessionContext, @Published

Monitoring Active Sessions

Access the current session ID and check for conflicts:

let currentId = sdk.state.deviceSessionId
let hasConflict = sdk.state.conflictingActiveSessionId != nil

On this page