The Plivo Browser SDK lets you make and receive calls via Plivo applications directly from any web browser that supports WebRTC. Using our SDK you can create applications like click to call, conferencing, and web phones. We support the most recent and previous 10 versions of Chrome and Firefox, and the most recent and previous five versions of Safari.
To see how to use Browser SDK, let’s walk through some simple code. Our example applications are a good introduction, and we call out some key points below.
Include the Plivo JavaScript file:
<script type="text/javascript" src="https://cdn.plivo.com/sdk/browser/v2.0.24/plivo.min.js"/>
The plivoBrowserSdk object needs to be initialized.
var options = {
"debug":"DEBUG",
"permOnClick":true,
"audioConstraints":{"optional":[{"googAutoGainControl":false},{"googEchoCancellation":false}]},
"enableTracking":true
};
var plivoBrowserSdk = new window.Plivo(options);
Pass function references to the events produced from the SDK. This is where your UI manipulation should handle all the different call flows.
plivoBrowserSdk.client.on('onWebrtcNotSupported', onWebrtcNotSupported);
plivoBrowserSdk.client.on('onLogin', onLogin);
plivoBrowserSdk.client.on('onLogout', onLogout);
plivoBrowserSdk.client.on('onLoginFailed', onLoginFailed);
plivoBrowserSdk.client.on('onCallRemoteRinging', onCallRemoteRinging);
plivoBrowserSdk.client.on('onIncomingCallCanceled', onIncomingCallCanceled);
plivoBrowserSdk.client.on('onCallFailed', onCallFailed);
plivoBrowserSdk.client.on('onCallAnswered', onCallAnswered);
plivoBrowserSdk.client.on('onCallTerminated', onCallTerminated);
plivoBrowserSdk.client.on('onCalling', onCalling);
plivoBrowserSdk.client.on('onIncomingCall', onIncomingCall);
plivoBrowserSdk.client.on('onMediaPermission', onMediaPermission);
plivoBrowserSdk.client.on('mediaMetrics',mediaMetrics);
plivoWebSdk.client.on('onConnectionChange',onConnectionChange);
Register using your Plivo endpoint credentials.
var username = 'johndoe12345';
var password = 'XXXXXXXX';
plivoBrowserSdk.client.login(username, password);
Make a call to any phone number or SIP endpoint. The application attached to a registered endpoint should have an answer URL that ensures that the correct <Dial>
element is returned.
var dest = “jane1234@phone.plivo.com”;
var extraHeaders = {'X-PH-Test1': 'test1', 'X-PH-Test2': 'test2'};
plivoBrowserSdk.client.call(dest, extraHeaders);
Answer an incoming call when an onIncomingCall
event is received.
plivoBrowserSdk.client.answer();
Send a DTMF tone when in a phone call.
plivoBrowserSdk.client.sendDtmf("1");
Here are the configuration parameters:
Attribute | Description | Allowed Values | Default Value |
---|---|---|---|
debug | Enable debug message in JS log | OFF, ERROR, WARN, INFO, DEBUG, ALL | INFO |
permOnClick | Set to true if you want to ask for mic permission before call. Otherwise, permission will be requested on page load. | true/false | false |
audioConstraints | Audio constraints object that will be passed to WebRTC getUserMedia(). | Audio constraints values are browser-specific. These values can be set to true or false: googEchoCancellation, googAutoGainControl, googNoiseSuppression, googHighpassFilter | {} |
enableTracking | Set to true if you want to get MediaMetrics events and enable call quality tracking. | true/false | true |
dscp | Set to true if you want to enable QoS in voice traffic. Differentiated Services field in the packet headers for all WebRTC traffic. | true/false | true |
preDetectOwa | Detect one-way audio before answering or sending the call. | true/false | false |
clientRegion | Initialization options to set and route calls to specific MediaServer PoPs | ["usa_west","usa_east","australia","europe","asia","south_america"] |
These are the methods supported on plivoBrowserSdk.client
</table>These methods are supported on plivoBrowserSdk.client.audio (supported only on Chrome).
Method | Description |
---|---|
login(username, password) | Register a Plivo endpoint |
logout() | Log out from the registered endpoint |
call(number, extraHeaders) | Call a number or SIP address. "number" takes a String value and "extraHeaders" takes a JSON object. Example of "extraHeaders": {'X-PH-Test1': 'test1', 'X-PH-Test2': 'test2'} |
answer() | Answer an incoming call |
hangup() | Hang up an ongoing call |
reject() | Reject an incoming call |
sendDtmf(digit) | Send digits as DTMF. "digit" can be any of the following one-character strings: "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "*", "#" |
mute() | Mute the mic |
unmute() | Unmute the mic |
setRingTone(url) | Set the ringtone that plays when calling. The default tone will be used if the URL is not specified. |
setRingToneBack(url) | Set the ringtone that plays when being called. The default tone will be used if the URL is not specified. |
setConnectTone(boolean) | The dial beep that will play until we get an alert response from network. Setting "false" will not play the beep tone. Default is "true," connect tone is enabled. |
setDebug(debug) | Set the log level of the SDK. Allowed values are OFF, ERROR, WARN, INFO, DEBUG, ALL. |
getPeerConnection() | Returns an RTCPeerConnection object. |
sendQualityFeedback(score, comment) | Send call quality feedback at the end of a call. "score" can take an integer value between 1 to 5. Comment must be one of the values: ['bad-audio', 'call-dropped', 'wrong-callerid', 'post-dial-delay', 'dtmf-not-captured', 'audio-latency', 'unsolicited-call', 'one-way-audio', 'no-audio', 'never-connected']. This feedback will be sent only if the "enableTracking" flag is set to true. |
getCallUUID() | Returns a String call UUID if a call is active, else returns null. |
getLastCallUUID() | Returns last call UUID. Useful if you want to send feedback for the last call. |
webRTC() | Returns true if WebRTC is supported and false if there is no WebRTC support. |
version | Variable that returns the current version of the Plivo SDK. |
isLoggedIn | Variable that returns true if the user is logged in and false otherwise. |
Method | Description |
---|---|
availableDevices(filter) | Promise-based callback that returns available devices. The filter parameter is optional and takes a String value. Pass "input" to filter by Input audio devices, "output" to filter by Output audio devices, null to get all audio devices. |
revealAudioDevices(arg) | Promise-based callback that forces and asks for allowing permission before returning all available devices. The arg parameter is optional and takes a string. Pass "returnStream" as arg to return local stream as well in Promise success. |
Objects in Audio Device API plivoBrowserSdk.client.audio are:
Object | Methods and Parameters | Description |
---|---|---|
microphoneDevices | set(deviceID) get() reset() | set method sets the audioDevice ID as default Microphone device. The deviceID parameter is mandatory and takes a String value.get method returns the Microphone device ID that’s set.reset method removes any Microphone device ID that’s set. |
speakerDevices | set(deviceId) get() reset() media(source) | set method sets the audioDevice ID as default Speaker device for DTMF and remote audio. The deviceID parameter is mandatory and takes a String value.get method returns the Speaker device ID that’s set.reset method removes any Speaker device ID that’s set.media takes "dtmf" or "ringback" as source parameter and returns the corresponding HTML audio element. This parameter is mandatory. |
ringtoneDevices | set(deviceId) get() reset() media() | set method sets the audioDevice ID as default Ringtone device for DTMF and remote audio. The deviceID parameter is mandatory and takes a String value.get method returns the Ringtone device ID that’s set.reset method removes any Ringtone device ID that’s set.media method returns Ringtone HTML audio element. |
plivoBrowserSdk object may receive these events:
Event | Description |
---|---|
onLogin | Occurs when a login is successful |
onLoginFailed(cause) | Occurs when a login has failed. cause returns the login failure reason |
onLogout | Occurs when a logout is successful |
onCalling | Occurs when a call is initiated |
onCallRemoteRinging | Occurs when the remote end starts ringing during an outbound call |
onCallAnswered | Occurs when an outbound or inbound call is answered |
onCallTerminated | Occurs when an outbound or inbound call has ended |
onIncomingCall(callerID, extraHeaders) | Occurs when there is an incoming call. callerID provides the caller ID and extraHeaders return the X-Headers from Plivo |
onIncomingCallCanceled | Occurs when an incoming call is cancelled by the caller |
onCallFailed(cause) | Occurs when an outbound or inbound call fails. cause returns the reason for call failing |
onMediaPermission(event) | Occurs when media permission has been granted. event returns the stream access status. The success event returns {'status':'success','stream':true} . On failure the event returns {'status':'failure','error':errorName} |
onWebrtcNotSupported | Occurs when browser does not support WebRTC |
mediaMetrics | Works only for Chrome. Raises the following events: high_jitter: when the jitter is higher than 30 ms for three out of last five samples. high_rtt: When the RTT is higher than 400 ms for three out of last five samples. high_packetloss: When the packet loss is > 10% for Opus and loss > 20% PCMU. low_mos: When sampled mos is < 3 for three out of last five samples. no_microphone_access When we detect one-way audio (<80 bytes sent in 3 seconds). no_audio_received : When the user is not able to hear the call recipient or when audio level is stable for last three samples. ice_timeout : Alert if ICE gathering takes more than 2 seconds either for outgoing call invite or incoming call answer. All the above will have `level`:`warning`, its `group` : `network/audio`, related params will have their own `value` and state `active`:`true/false`. Example: {active : true, desc : "local_audio", group : "audio", level : "warning", type : "no_audio_received", value : 0} |
onAudioDeviceChange(deviceObj) | Occurs when there is a change in USB audio device: device added or removed. This event emits an object with two properties, "change" and "device." "change" may have values "added" or "removed". "device" provides device-specific properties |
onConnectionChange | Generated when the state of Plivo’s WebSocket connection changes — for example, when the WebSocket is disconnected due to internet issues. On WebSocket disconnect { On WebSocket reconnect { Common WebSocket event codes (RFC 6455) 1006: indicates that the connection was closed abnormally, e.g., without sending or |