Opentact Javascript SDK
Installation Guide (ES6)
If you are developing a ES6 app (eg. React, Vue), you can easily include the sdk:
npm install opentact --save
in order to add the module dependency to your app
import {Opentact} from 'opentact-js-sdk'
in order to start using the client in your javascript code.
Installation (Browser)
If you are developing in plain Javascript and not using modules, you can simply include the opentact.min.js
library in your html via a script tag.
<html>
...
<body>
...
<script src="/path/to/your/opentact.min.js" charset="utf-8"/>
...
</body>
</html>
IMPORTANT: Please keep in mind the charset attribute if you use the minified version of the SDK. This is required by the embbeded sip.js library.
User Guide
Creating a new client
It is a simple as:
var client = new Opentact();
Logging in
In order to log in, you need to obtain an identity uuid and a session token.
client.on( "connection", function(conn) {
console.log( "sip registered", conn.sip );
console.log( "xmpp connected", conn.xmpp );
});
client.login({
identity: "<<your identity uuid>>",
token: "<<your session token>>"
});
Starting a new call
client.on( "call:starting", function(session) {
console.log( "session id: ", session.id );
console.log( "call with: ", session.remoteUser );
console.log( "has audio: ", session.audio );
console.log( "has video: ", session.video );
});
client.call({
to: "<<an identity uuid>>"
audio: true,
video: true
});
Managing call lifecycle
client.on( "call:ringing", function(session) {
...
});
client.on( "call:rejected", function(session) {
...
});
client.on( "call:started", function(session) {
...
});
client.on( "call:terminated", function(session) {
console.log( "reason: ", session.reason );
});
Accepting, rejecting and terminating a call
client.on( "call:received", function(session) {
client.accept({ id: session.id });
});
client.on( "call:started", function(session){
client.hangup({ id: session.id });
});
Handling IM events
client.on( "im:sending", function(msg) {
console.log( "from: ", msg.from );
console.log( "to: ", msg.to );
console.log( "body: ", msg.body );
console.log( "session", msg.session );
console.log( "state", msg.state );
});
client.on( "im:receipt", function(msg) {
...
});
client.on( "im:received", function(msg) {
...
});
client.on( "im:sent", function(msg) {
...
});
Starting a new IM session and posting a message
client.on( "im:started", function(session) {
console.log( "session id: ", session.id );
console.log( "im remote user: ", session.remoteUser );
console.log( "im local user: ", session.localUser );
console.log( "is im: ", session.im );
client.imMessage({
to: session.remoteUser,
message: "Hello"
});
});
client.im({
to: "<<an identity uuid>>"
});
Receiving typing indicators
You can listen for chat states easily by registering a event listener.
See https://xmpp.org/extensions/xep-0085.html for more info.
client.on( "im:state", function(msg) {
console.log( "from", msg.from );
console.log( "session", msg.session);
console.log( "is typing", msg.state === 'composing' );
console.log( "is paused", msg.state === 'paused' );
});
Sending typing indicators
Chat state indicators are sent to a session, rather than to an identity:
client.imState({
session: "a session id",
state: "composing"
});
client.imState({
session: "a session id",
state: "paused"
});
Handling IM errors
Errors have a code and a condition:
client.on( 'im:error', function(error) {
console.log( "session id", error.session );
console.log( "message id", error.id );
console.log( "code", error.code );
console.log( "condition", error.condition);
});
Developer Guide
You need either npm
installed. Then:
- clone this repo
npm install
This will download the source code for this lib and grab all dependencies. Then, you've got the following scripts:
npm run build
- produces production version of the sdk in the lib
folder
npm run dev
- produces development version of your library and runs a watcher
npm run test
- well ... it runs the tests :)
npm run test:watch
- same as above but in a watch mode