@cpaassdk/cpaas-sdk
Version:
121 lines (85 loc) ⢠3.46 kB
Markdown
<br>
Chrome
<br>
npm i @cpaassdk/cpaas-sdk
<br>
The CPaaSAPI SDK for Web is available for download with NPM as well as via a script tag.
```
npm i -S @cpaassdk/cpaas-sdk
```
You can import the CPaaSAPI JavaScript library as follows.
```javascript
import { cpaasActions, voice } from 'cpaas-api'
```
To start using the SDK, you should register it by using the cpaasActions.register() method, along with the parameters below.
```javascript
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.
If the registration process was successful, the following events will take place:
1. Authorization ā Our servers will authenticate and authorize the use of the SDK
2. 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.
To initiate a call simply call **voice.create()**. and **voice.connect(...)**. <br/>
``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.
```javascript
callId = await voice.create()
call = await voice.connect(callId, callOptions)
```
`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.
```javascript
call.mute() //mute the current call
call.unmute() // unmute current call
call.end() //end the current 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.
```javascript
const cpaasEvents = await cpaasActions.register(registrationSettings)
cpaasEvents.addListener("incomingCall", async (e) => {...})
```
You can accept the call by calling ``voice.connect(callId, callOptions)``
```javascript
let currentCall;
cpaasEvents.addListener("incomingCall", async (e) => {
currentCall = await voice.connect(e.callId, {
audio: true
})
}
```
You can listen to various call events such as: call connected, call ringing, connection failed, call ended, call reconnecting.
```javascript
//ringing event
call.onRinging.addEventListener(_ => console.log("ringing on destination"))
//call ended event
call.onCallEnd.addEventListener(event => console.log("call has ended", event.CPaaSReason ))
```
[](https://click2call.cpaasapi.com/)
<br>