PbxApi

This document describes the PbxApi object in the JavaScript environment for app serivices.

PBX APIs are used on incoming AppWebsocket connections from app objects on the PBX to the app service. You can use any PBX API in your app service that is activated at the app object in the PBX.

Table of content

Object PbxApi
Functions onconnected

Object PbxApiConnection
Members api
domain
pbx
pbxDns
version
build
info
Functions onmessage
onclose
send
setFlowControl
messageComplete
encrypt
decrypt
hash
create
close

PbxApi

PbxApi
Constructor for creating a PBX API object.

Parameters

string apiThe identifier for the API.
var replicationApi = new PbxApi("PbxTableUsers");
onconnected
Registers a callback function that is invoked when a new AppWebsocket from a PBX is connected that supports the API.

Parameters

function callback(PbxApiConnection conn)The conn parameter passed with the callback can be used to send and receive API messages on the connection.

Return value

PbxApiA reference to the object itself. Useful for method chaining.
new PbxApi("PbxTableUsers").onconnected(function(conn) {
    log("new connection with PbxTableUsers support");
});

PbxApiConnection

api
The ID of the API.
domain
The domain of the connected PBX.
pbx
The name of the connected PBX.
pbxDns
The DNS name of the connected PBX.
version
The version of the PBX.
build
The build number of the PBX.
info
A string containing the info of the app object the AppWebsocket connection was established from. It contains a JSON structure that includes additional info like the app object name or number of service licenses assigned to this app object.
onmessage
Registers a callback function that is invoked when receiving an API message

Parameters

function callback(string msg)A string containg a JSON structure. Must be parsed using JSON.parse.

Return value

PbxApiConnectionA reference to the object itself. Useful for method chaining.
new PbxApi("PbxApi").onconnected(function(conn) {
    conn.send(JSON.stringify({ api: "PbxApi", mt: "SubscribePresence", sip: "johndoe" }));

    conn.onmessage(function(msg) {
        var obj = JSON.parse(msg);
        if (obj.mt == "SubscribePresenceResult") {
            conn.send(JSON.stringify({ api: "PbxApi", mt: "UnsubscribePresence", src: obj.src }));
        }
    });
});
onclose
Registers a callback function that is invoked when the AppWebsocket connection was closed by the PBX.

Parameters

function callback()The callback that shall be invoked when the connection was closed.

Return value

PbxApiConnectionA reference to the object itself. Useful for method chaining.
new PbxApi("PbxTableusers").onconnected(function(conn) {
    log("connected");
    conn.onclose(function() {
        log("closed");
    });
});
send
Sends a JSON message.

Parameters

string messageA string (max. length 65535) containing a JSON structure that represents the API message.

Return value

PbxApiConnectionA reference to the object itself. Useful for method chaining.
conn.send(JSON.stringify({ api: "PbxApi", mt: "SubscribePresence", sip: "john.doe" }));
setFlowControl
Tells the library that it wants to use flow control. That means that after receiving the onmessage callback, the library should deliver no more messages until the application has called messageComplete

Parameters

bool ontrue if flow control shall be used. false if flow control should not be used.

Return value

PbxApiConnectionA reference to the object itself. Useful for method chaining.
new PbxApi("PbxApi").onconnected(function(conn) {
    conn.setFlowControl(true)
        .onmessage(function(msg) {
            var obj = JSON.parse(msg);
            if (obj.mt == "SubscribePresenceResult") {
                // do something
            }
            conn.messageComplete();
        });
});
messageComplete
Tells the library that the application is ready to receive the next message using the onmessage callback.

Return value

PbxApiConnectionA reference to the object itself. Useful for method chaining.
new PbxApi("PbxApi").onconnected(function(conn) {
    conn.setFlowControl(true)
        .onmessage(function(msg) {
            var obj = JSON.parse(msg);
            if (obj.mt == "SubscribePresenceResult") {
                // do something
            }
            conn.messageComplete();
        });
});
encrypt
Encrypts a string using the shared secret of the connection and a random seed.

Parameters

string seedA seed value that shall be unique for each call. The seed is needed by the remote party for decryption.
string dataThe string to be encrypted.

Return value

stringThe encrypted string.
var seed = "some seed";
var secret = "some secret text";
var encryptedText = conn.encrypt(seed, secret);
conn.send(JSON.stringify({ mt: "SomeMessage", seed: seed, encryptedText: encryptedText }));
decrypt
Decrypts a string using the shared secret of the connection and a random seed.

Parameters

string seedThe seed needs to be given by the remote party for decryption.
string dataThe encrypted string.

Return value

stringThe decrypted string.
conn.onmessage(function(msg) {
    var obj = JSON.parse(msg);
    if (obj.mt == "SomeMessage") {
        var secret = conn.decrypt(obj.seed, obj.encryptedText);
    }
});
hash
Calculates a hash value using the shared secret of the connection and a random seed.

Parameters

string seedA seed value that shall be unique for each call.
string dataThe string to be hashed.

Return value

stringThe calculated hash value.
var seed = "some seed";
var text = "some secret text";
var hash = conn.hash(seed, secret);
conn.send(JSON.stringify({ mt: "SomeMessage", text: text, seed: seed, hash, hash }));
create
Creates a new PbxApiConnection object, that uses the same underlying websocket connection, but with a different target API. This mechanism can be used along with the PbxImpersonation API.

Parameters

string targetApiThe target API value, as returned with the CreateApiResult message of the PbxImpersonationApi

Return value

PbxApiConnectionThe new connection object.
new PbxApi("PbxImpersonation").onconnected(function(conn) {
    conn.send(JSON.stringify({ api: "PbxImpersonation", mt: "CreateApi", sip: "johndoe", target: "PbxApi" }));

    conn.onmessage(function(msg) {
        var obj = JSON.parse(msg);
        if (obj.mt == "CreateApiResult") {

            // create new PbxApiConnection object with the returned target API name
            var targetApi = obj.target;
            var pbxApiConn = conn.create(targetApi);
            
            pbxApiConn.onmessage(function(msg) { });

            pbxApiConn.send(JSON.stringify({ api: targetApi, mt: "SubscribeProfile", sip: "johndoe" }));
        }
    });
});
close
Closes the PbxApiConnection object. This should be done if the application is not or no longer interested in using the related PbxApi on that connection. Please note that the underlying websocket connection is not affected and stays connected.

Return value

new PbxApi("PbxApi").onconnected(function(conn) {
    if (pbx != "master") conn.close();
});