@cpaassdk/cpaas-sdk v1.0.10
@cpaassdk/cpaas-sdk
Browsers
Chrome
Installation
npm i @cpaassdk/cpaas-sdk
Getting the CPaaSAPI SDK
The CPaaSAPI SDK for Web is available for download with NPM as well as via a script tag.
Downloading from NPM
npm i -S @cpaassdk/cpaas-sdk
Importing the Library into Your Project
You can import the CPaaSAPI JavaScript library as follows.
import { cpaasActions, voice } from 'cpaas-api'
Initializing the SDK
To start using the SDK, you should register it by using the cpaasActions.register() method, along with the parameters below.
const cpaasEvents = await cpaasActions.register({
customDomain: "api.your-organization.com",
accountSid: "xxxxxx",
accountToken: "xxxxxx",
appSid: "xxxxxx",
clientId: "xxxxxx",
pnsToken: "xxxxxx",
baseUrl: "https://xxxxxx.com"
})
š” Note:
- This should be done once in a session
- We recommend to do it as soon as you open the application, it will save you time on the call connection.
Authorization and Web Socket Connection
If the registration process was successful, the following events will take place:
- Authorization ā Our servers will authenticate and authorize the use of the SDK
- Web socket connection initialization ā the SDK will initialize the web socket connection.
š” NOTE: The steps above must be successfully completed. Failure in any of the above procedures will prevent the SDK from working properly.
Starting a Call
To initiate a call simply call voice.create(). and voice.connect(...).
create()
: creates and returns a call id which you can use or share with others
connect()
: starts the actual connection to the call and returns an api call obj.
callId = await voice.create()
call = await voice.connect(callId, callOptions)
The Call Object
Call Object
is an API object that return from the connect()
method.
The 'Call' lets you perform actions on an active call such as muting or ending the call.
call.mute() //mute the current call
call.unmute() // unmute current call
call.end() //end the current call
...
Answering an Incoming Call
To answer an incoming call, you need to listen to event: cpaasEvents.addListener
.
When there is an incoming call event, you can use voice.connect(callId, callOptions)
to answer it with, using the given callId.
Note: You should not use the create()
method in this case.
const cpaasEvents = await cpaasActions.register(registrationSettings)
cpaasEvents.addListener("incomingCall", async (e) => {...})
You can accept the call by calling voice.connect(callId, callOptions)
let currentCall;
cpaasEvents.addListener("incomingCall", async (e) => {
currentCall = await voice.connect(e.callId, {
audio: true
})
}
Listening to Call Events
You can listen to various call events such as: call connected, call ringing, connection failed, call ended, call reconnecting.
//ringing event
call.onRinging.addEventListener(_ => console.log("ringing on destination"))
//call ended event
call.onCallEnd.addEventListener(event => console.log("call has ended", event.CPaaSReason ))