Listen for State Changes and Events

Implement SCSChatSessionDelegate to be notified about state changes made before, during, and after a chat session. This delegate also allows you to listen for error conditions so you can present alerts to the user when applicable.

The legacy chat product is scheduled for retirement on February 14, 2026, and is in maintenance mode until then. During this phase, you can continue to use chat, but we no longer recommend that you implement new chat channels. To avoid service interruptions to your customers, migrate to Messaging for In-App and Web before that date. Messaging offers many of the chat features that you love plus asynchronous conversations that can be picked back up at any time. Learn about chat retirement in Help.

Important

Listening to State Changes 

A chat session can be in one of the following states:

Inactive

No active session.

Connecting

A connection with chat servers is being established.

Queued

A connection has been established, and is now in the queue for next available agent.

Connected

Connected with an agent .

Ending

Session is cleaning up the connection at the end of a session.

These states are defined in SCSChatSessionState.

Throughout a session, your application might want to know the current state. You can monitor state changes by implementing SCSChatSessionDelegate. Use the add(delegate:) method on SCSChat to register your delegate. Use the session(didTransitionFrom:to:) method to listen for state changes.

Handling Session Termination and Error Conditions 

The SDK doesn’t present UI alerts for session termination or error conditions so you’ll need to listen for these events and decide what to show your users. There are two SCSChatSessionDelegate methods for this purpose:

  1. To track session termination, use the session(didEnd:) method. Inspect the reason (SCSChatSessionEndEvent.type) to determine why the session stopped. Use the SCSChatSession object to get the session ID and other session information.
  2. You can track errors with the session(didError:fatal:) method. Compare the error code to SCSChatErrorCode to determine what kind of error occurred.

Example 

This sample code does the following:

1import UIKit
2import ServiceCore
3import ServiceChat
4
5class MyChatSessionDelegateImplementation: NSObject, SCSChatSessionDelegate {
6
7  // TO DO: Register this delegate using
8  //        ServiceCloud.shared().chatCore.add(delegate: myDelegate)
9
10  /**
11   Delegate method to handle state change.
12   */
13  func session(_ session: SCSChatSession!,
14               didTransitionFrom previous: SCSChatSessionState,
15               to current: SCSChatSessionState) {
16
17    NSLog("Chat state changed...")
18
19    switch current {
20      case .connecting:
21        NSLog("Chat now connecting...")
22      case .connected:
23        NSLog("Chat connected...")
24      // TO DO: Handle other reasons
25      default:
26        break
27      }
28  }
29
30  /**
31   Delegate method for session stop event.
32   */
33  func session(_ session: SCSChatSession!, didEnd endEvent: SCSChatSessionEndEvent!) {
34
35    var reason = "Unknown"
36
37    switch endEvent.reason {
38      case .agent:
39        reason = "The agent has ended the session."
40      case .noAgentsAvailable:
41        reason = "No agents were available."
42      default:
43        // TO DO: Handle other reasons
44        break
45    }
46
47    NSLog("\nChat End Session. Reason: \(reason)")
48
49    // You can access the session ID from the SCSChatSession object
50    let sessionId = session.sessionId
51  }
52
53  /**
54   Delegate method for error conditions.
55   */
56  func session(_ session: SCSChatSession!, didError error: Error!, fatal: Bool) {
57    // TO DO: Handle error condition
58    NSLog("Chat error: \(error.localizedDescription)")
59  }
60}