If you are developing a ES6 app (eg. React, Vue), you can easily include the sdk:
-
npm install opentact-js-sdk --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.
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>
...
<!-- opentact sdk -->
<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.
var client = new Opentact({
sipProxy: "158.69.112.28",
sipWsUrl: "wss://158.69.112.28:8443",
sipDebug: true,
xmppWsUrl: "wss://xmpp.opentact.org:5280/websocket",
xmppHost: "xmpp.opentact.org",
xmppDebug: true,
xmppAutoReconnect: true
});
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>>"
});
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 );
// update your UI here
});
client.call({
to: "<<an identity uuid>>"
audio: true,
video: true
});
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 );
});
client.on( "call:received", function(session) {
client.accept({ id: session.id }); // or client.reject
});
client.on( "call:started", function(session){
client.hangup({ id: session.id });
});
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) {
...
});
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 );
// once the session is ready,
// we can start chatting
client.imMessage({
to: session.remoteUser,
message: "Hello"
});
});
client.im({
to: "<<an identity uuid>>"
});
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' );
});
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"
});
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);
});
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 thelib
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