WebSocket

You can use WebSocket to connect to the PBX. It allows bidirectional communication between the browser and the PBX.

It is mainly used for call control (including WebRTC calls), call status, other extension information as well as status information about all other relevant extensions and accounts. For example, it gives information about other extensions in the same domain whether an extension is registered or not, or whether it is in a call or idle or whether its available for chat etc.

Connecting to the PBX

Connect WebSocket to the PBX using the URL:

ws://YOURPBXURL/websocket?domain=YOURDOMAIN&user=EXTENSION

OR

wss://YOURPBXURL/websocket?domain=YOURDOMAIN&user=EXTENSION

EXTENSION is the extension for which the relevant information will be provided and on which calls can be made and received. Live status information about the other extensions in the same domain will also be provided when something changes.

Example in JS:

var dest = "wss://192.168.1.11/websocket?domain=localhost&user=45";
this.socket = new WebSocket(dest);
this.socket.onopen = function (evt) { onOpen(evt) };
this.socket.onclose = function (evt) { onClose(evt) };
this.socket.onmessage = function (evt) { onMessage(evt) };
this.socket.onerror = function (evt) { onError(evt) };

The callback functions like onxxxx() will be called on those events.

For example when there is a message from the PBX, onMessage(evt) will be called. You can then parse the evt.data which is a JSON object to extract and display the relevant information.

Similarly, commands can also be sent to the PBX using the command:

this.socket.send(message);

Receiving messages in onMessage callback:

The onMessage callback is called when there is a message from the PBX. And the message is the argument of the function, say event:

function onMessage (event) {

    Process the events here

}

The event.data message should be parsed as JSON format as below:

{ action: actionname, ... }

 

Some of the information you can get:

action: domain-state

accounts:  Gives the status of all the accounts (not just yours) in the domain, like whether it is registered, DND, available for chat, in a call etc.

calls: The details of the active calls

...

Example:

{ action: "domain-state", messages: , calls: Array(1), accounts: Array(26) }

 

action: user-state

type: extensions

account: your extension

registered: true/false

chatstatus: online/offline

dnd: true/false

...

Example:

{ action: "user-state", type: "extensions", account: "45", domain: "localhost", chatstatus: "online", … }

 

action: call-state

id: Id of the call to be identified for any action that may be taken

from: Who made the call

to: To whom it was made (your extension)

callstate: connected/ringing etc.

...

Example:


  action: "call-state",
  calls: { id: "call id", from: "call from", to: "call-to", callstate: "state of the call like connected etc.", and so on ...}
}