TelnyxClient is the main entry point for interacting with the Telnyx WebRTC SDK. It handles connection management, call creation, and responses from the Telnyx platform.
Core Functionalities
- Connection Management: Establishes and maintains a WebSocket connection to the Telnyx RTC platform.
- Authentication: Supports authentication via SIP credentials or tokens.
- Call Control: Provides methods to initiate (
newInvite), accept (acceptCall), and end (endCall) calls. - Event Handling: Uses
TxSocketListenerto process events from the socket, such as incoming calls (onOfferReceived), call answers (onAnswerReceived), call termination (onByeReceived), and errors (onErrorReceived). - State Exposure: Exposes connection status, session information, and call events via
SharedFlow(recommended:socketResponseFlow) and deprecatedLiveData(e.g.,socketResponseLiveData) for UI consumption.
Key Components and Interactions
TxSocket: Manages the underlying WebSocket communication.TxSocketListener: An interface implemented byTelnyxClientto receive and process socket events. Notably:onOfferReceived(jsonObject: JsonObject): Handles incoming call invitations.onAnswerReceived(jsonObject: JsonObject): Processes answers to outgoing calls.onByeReceived(jsonObject: JsonObject): Handles call termination notifications. ThejsonObjectnow contains richer details includingcause,causeCode,sipCode, andsipReason, allowing the client to populateCallState.DONEwith a detailedCallTerminationReason.onErrorReceived(jsonObject: JsonObject): Manages errors reported by the socket or platform.onClientReady(jsonObject: JsonObject): Indicates the client is ready for operations after connection and initial setup.onGatewayStateReceived(gatewayState: String, receivedSessionId: String?): Provides updates on the registration status with the Telnyx gateway.
CallClass: Represents individual call sessions.TelnyxClientcreates and manages instances ofCall.CallState: The client updates theCallStateof individualCallobjects based on socket events and network conditions. This includes states likeDROPPED(reason: CallNetworkChangeReason),RECONNECTING(reason: CallNetworkChangeReason), andDONE(reason: CallTerminationReason?)which now provide more context.socketResponseFlow: SharedFlow<SocketResponse<ReceivedMessageBody>>: This SharedFlow stream is the recommended approach for applications. It emitsSocketResponseobjects that wrap messages received from the Telnyx platform. ForBYEmessages, theReceivedMessageBodywill contain acom.telnyx.webrtc.sdk.verto.receive.ByeResponsewhich is now enriched with termination cause details.socketResponseLiveData: LiveData<SocketResponse<ReceivedMessageBody>>: [DEPRECATED] This LiveData stream is deprecated in favor ofsocketResponseFlow. It’s maintained for backward compatibility but new implementations should use SharedFlow.
Usage Example
Recommended approach using SharedFlow:Telnyx Client
NOTE: Remember to add and handle INTERNET, RECORD_AUDIO and ACCESS_NETWORK_STATE permissions
Initialize
To initialize the TelnyxClient you will have to provide the application context.Connect
Once an instance is created, you can call the one of two available .connect(…) method to connect to the socket.- providedServerConfig, the TxServerConfiguration used to connect to the socket
- txPushMetaData, the push metadata used to connect to a call from push (Get this from push notification - fcm data payload) required fot push calls to work
- credentialConfig, represents a SIP user for login - credential based
- autoLogin, if true, the SDK will automatically log in with the provided credentials on connection established. We recommend setting this to true.
- providedServerConfig, the TxServerConfiguration used to connect to the socket
- txPushMetaData, the push metadata used to connect to a call from push (Get this from push notification - fcm data payload) required fot push calls to work
- tokenConfig, represents a SIP user for login - token based
- autoLogin, if true, the SDK will automatically log in with the provided credentials on connection established. We recommend setting this to true.
Listening for events and reacting
We need to react for a socket connection state or incoming calls. We do this by getting the Telnyx Socket Response callbacks from our TelnyxClient. Recommended approach using SharedFlow:SocketResponse and ReceivedMessageBody.
- @see [SocketResponse]
- @see [ReceivedMessageBody]
SocketResponse and ReceivedMessageBody.
- @see [SocketResponse]
- @see [ReceivedMessageBody]
SocketObserver.
In onMessageReceived we will receive objects of ReceivedMessageBody class.