The JS library for develop WebSocket/WebRTC phone apps based on lirax.net (Phone Cloud System)
In a browser:
<script src="dist/xphone.js"></script>
CDN:
<script src="https://cdn.jsdelivr.net/npm/xphone@latest/dist/xphone.js"></script>
Using npm:
npm install xphone --save
ES6
import XPhone from 'xphone';
/* Initialization */
const phone = new XPhone();
/* Call to echo-test */
phone.onOpen = () => {
phone.makeCall("200");
};
/* Connection closed */
phone.onClose = () => console.log("Connection closed");
/* Icomming call */
phone.onCreate = call => {
if (call.type === phone.INCOMING) {
setTimeout(() => phone.acceptCall(call.line), 3000);
}
};
/* Handling errors */
phone.onError = error => console.log("error", error);
/* Change the call parameters */
phone.onChange = call => {
console.log("change", call);
};
/* Destroying the call */
phone.onDestroy = call => {
console.log("destroy", call);
};
/* Authorization to LiraX */
phone.init({
login: "1011011",
password: "mypassword"
});
# copy config file
cp .env.example.js .env.js
# install dependencies
npm install
# build for production with minification
npm run build
# build for production and view the bundle analyzer report
npm run build --report
# run all tests
npm test
npm test
Tested in Chrome 54-61, Firefox 49-56
Connection to the service
Parameters
Name | Type | Description |
---|---|---|
credentials | Object | Object with parameters (see below) |
credentials.login | String | SIP number |
credentials.password | String | SIP password |
Example
phone.init({
login: '1111111',
password: 'secret_password',
});
Close the connection
Example
phone.close();
Check the connection
Returns
Type | Description |
---|---|
Boolean | true if the WebSocket connection is established, false otherwise |
Example
phone.isOpen() && console.log('Connection established');
Make a call
Parameters
Name | Type | Description |
---|---|---|
phoneNumber | String | A Phone Number |
Returns
Type | Description |
---|---|
Integer | The number of line |
Example
let line = phone.makeCall('380442388744');
Hungup a call
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
Example
let line = phone.makeCall('380442388744');
setTimeout(() => phone.finishCall(line), 10000);
Accept a call
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
Example
phone.onCreate = call => {
if (call.type === phone.INCOMING) {
setTimeout(() => phone.acceptCall(call.line), 10000);
}
};
Send a DTMF
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
symbol | String | The symbol 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, *, # |
Example
phone.onCreate = call => {
setTimeout(() => {
phone.sendDTMF(call.line, '5');
phone.sendDTMF(call.line, '7');
phone.sendDTMF(call.line, '9');
phone.sendDTMF(call.line, '#');
}, 5000);
};
Hold a call
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
Example
phone.onCreate = call => {
const line = call.line;
setTimeout(() => phone.holdCall(line), 10000);
};
Enable conferencing mode
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
Example
phone.onCreate = call => {
const line = call.line;
setTimeout(() => phone.conferenceCall(line), 3000);
};
Enable redirection mode
Parameters
Name | Type | Description |
---|---|---|
line | Integer | The number of line |
Example
// Call forwarding 380442388744 to 380442388745
const lineFoo = phone.makeCall('380442388744');
const lineBar = phone.makeCall('380442388745');
setTimeout(() => {
phone.forwardCall(lineFoo);
phone.forwardCall(lineBar);
}, 5000);
Returns the array of all active calls
Returns
Type | Description |
---|---|
Array | The Array of calls |
Example
console.log(phone.getCalls());
Connection is established
Example
phone.onOpen = () => console.log('Connection is established');
Connection is closed
Example
phone.onClose = () => console.log('Connection is closed');
Application error
Parameters
Name | Type | Description |
---|---|---|
error | Object | The Error message |
Example
phone.onError = error => console.log(error);
The call is created
Parameters
Name | Type | Description |
---|---|---|
call | Object | The Object with call properties (see below) |
call.line | Integer | The number of line |
call.startDate | Object | The date of start call |
call.connectDate | Object | The date of connection call |
call.phoneNumber | String | The phone number |
call.type | Integer | The type of call (0 - incoming, 1 - outgoing) |
call.hold | Boolean | The hold mode of call, true if the hold mode is enabled, false otherwise |
call.conference | Boolean | The conference mode of call, true if the hold mode is enabled, false otherwise |
call.forward | Boolean | The forward mode of call, true if the hold mode is enabled, false otherwise |
call.file | String | Link to the voice file |
Example
phone.onCreate = call => {
console.log(call);
};
The properties of the call have changed
Parameters
Name | Type | Description |
---|---|---|
call | Object | The Object with call properties (see onCreate event) |
Example
phone.onChange = call => {
console.log(call);
};
Call ended
Parameters
Name | Type | Description |
---|---|---|
call | Object | The Object with call properties (see onCreate event) |
Example
phone.onDestroy = call => {
console.log(call);
};
(String) The URL to which to connect, Default: 'wss://lirax***:1887'
Example
phone.wsURL = 'wss://test.com:1887';
(String) External number, Default: ''
Example
phone.callOut = '380001234567';
(Boolean) Automatic reconnection when disconnected, Default: true
Example
phone.reConnect = false;