UNPKG

@cpaassdk/cpaas-sdk

Version:
121 lines (85 loc) • 3.46 kB
# @cpaassdk/cpaas-sdk <br> # Browsers Chrome <br> # Installation npm i @cpaassdk/cpaas-sdk <br> ## 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. ```javascript 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. ```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. ### Authorization and Web Socket 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. ## Starting a Call 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) ``` ### 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. ```javascript 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. ```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 }) } ``` ### Listening to Call Events 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 )) ``` # Demo [Demo app](https://click2call.cpaasapi.com/) <br>