Configure WebSockets

A WebSocket is a bidirectional TCP connection between a client and server that's kept open until the app closes it. WebSockets can be configured to work with our authentication features and products like Agentforce Speech Foundation.

With a WebSocket, an app uses HTTP to make an initial connection. The connection then gets upgraded to a TCP socket-based connection.

On Android, Mobile SDK uses okhttp3.OkHttpClient.newWebSocket(okhttp3.Request, okhttp3.WebSocketListener) to return instances of okhttp3.WebSocket. See Square's documentation: https://square.github.io/okhttp/.

On iOS, Mobile SDK uses URLSessionWebSocketTask, available through Apple's URLSession API. See Apple's documentation: https://developer.apple.com/documentation/foundation/urlsessionwebsockettask.

To create a WebSocket connection, use one of these RestClient methods. The methods return a ready-to-use WebSocket client instance that handles token injection and retry logic internally.

On iOS, you have the option to create a connection from a URLRequest or a RestRequest.

To send data to a Salesforce API endpoint, use the send method. Internally, send automatically refreshes the token if authentication fails and retries once with a new token before surfacing an error.

WebSockets receive data by listening for incoming messages. Our listen methods:

  • Automatically resume the WebSocket task
  • Continuously listen for new messages
  • Refresh the token and reestablish the connection once, if needed

To receive data on Android, use the WebSocketListener adapter object.

To receive data on iOS, use the listen(onReceive:) method.