ChatProvider

@MainActor
public protocol ChatProvider : AnyObject, Sendable

The interface for interacting with chat features of the CXone platform.

  • The handler for the logs occured in CXone chat.

    Declaration

    Swift

    @MainActor
    static var logWriter: LogWriter? { get set }
  • Current chat state of the SDK

    The state defines whether it is necessary to set up the SDK, connect to the CXone services or start communication with an agent.

    Declaration

    Swift

    @MainActor
    var state: ChatState { get }
  • Chat mode based on the channel configuration

    Declaration

    Swift

    @MainActor
    var mode: ChatMode { get }
  • The provider for connection related properties and methods.

    Declaration

    Swift

    @MainActor
    var connection: ConnectionProvider { get }
  • The provider for customer related properties and methods.

    Declaration

    Swift

    @MainActor
    var customer: CustomerProvider { get }
  • The provider for customer chat fields related properties and methods.

    Declaration

    Swift

    @MainActor
    var customerCustomFields: CustomerCustomFieldsProvider { get }
  • The provider for thread related properties and methods.

    Declaration

    Swift

    @MainActor
    var threads: ChatThreadListProvider { get }
  • The provider for report related properties and methods.

    Declaration

    Swift

    @MainActor
    var analytics: AnalyticsProvider { get }
  • The provider for handling user responses to proactive actions such as inactivity popups, custom automation popups, and future automated engagement features.

    Use this provider to send user interaction results (e.g., “Continue”, “Close”, etc.) for proactive UI elements back to the backend.

    Declaration

    Swift

    @MainActor
    var proactiveAction: ProactiveActionProvider { get }
  • Returns a new AsyncStream of all SDK events.

    Each call produces an independent stream — multiple components in your app can each subscribe and all receive every event. There is no fan-in or de-duplication across subscribers; cancelling one task does not affect the others.

    let observation = Task { [chatProvider] in
        for await event in chatProvider.events() {
            switch event {
            case .threadUpdated(let thread): /* handle */ break
            case .error(let error): /* surface to user */ break
            default: break
            }
        }
    }
    // Later, to stop observing:
    observation.cancel()
    

    Lifecycle

    • The stream never finishes with an error. SDK errors arrive as the error(_:) case — handle it as you would any business-level error (surface to the user, log, retry, etc.).
    • Calling signOut() finishes every active stream cleanly — each for await loop exits normally with no error.
    • To stop observing without signing out, cancel the wrapping Task.
    • Each stream buffers at most 64 events; if the consuming Task falls behind, the oldest buffered events are silently dropped. Keep the for await body lightweight or hand off heavy work via Task { }.

    Declaration

    Swift

    @MainActor
    func events() -> AsyncStream<ChatEvent>
  • Signs the customer out, disconnects from the CXone service, and clears the session.

    Calling this method:

    1. Disconnects the WebSocket cleanly (does not emit unexpectedDisconnect).
    2. Purges the SDK’s keychain entries and UserDefaults state.
    3. Discards the in-memory session state — the loaded threads and their messages, the customer and per-contact custom fields, the channel configuration, and the brand, channel and environment the session was prepared against. state returns to initial.
    4. Finishes every active events() stream — observers see their for await loop exit normally.

    The device token and any listener registered with setTokenDelegateListener(_:) are deliberately kept: both belong to the application rather than to the chat session, so they stay valid for the next one.

    The instance remains alive and reusable after signOut(). You can call prepare(environment:brandId:channelId:) and connect() again to start a new session with fresh credentials. If you prefer a clean slate, discard this instance and construct a new CXoneChat.

    Declaration

    Swift

    @MainActor
    func signOut()