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
Functions onmessage
onclose
send
setFlowControl
messageComplete

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.
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 }));
        }
    });
});
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 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();
        });
});