Skip to content

Instantly share code, notes, and snippets.

@dtolb
Last active September 18, 2024 19:32
Show Gist options
  • Select an option

  • Save dtolb/76b8fb5ab09b8c0ad911fd1760b6ebb9 to your computer and use it in GitHub Desktop.

Select an option

Save dtolb/76b8fb5ab09b8c0ad911fd1760b6ebb9 to your computer and use it in GitHub Desktop.
js sip

JSSIP with Bandwidth API

Prerequisites

  • Register for Bandwidth Application Platform account here
  • Register a SIP domain
  • Create an endpoint/user
  • If you want to make calls to the PSTN (normal phones) you will need a server to handler events from Catapult
  • Make phone calls

For a more in depth guide, view this article

Quick Start

Full docs are here

CDN hosted library: minified not-minified

JSFIDDLE Demo Client

Outbound Call

var callOptions = {
  mediaConstraints: {
    audio: true, // only audio calls
    video: false
  }
};

var bwPhone = new JsSIP.UA({
  'uri': 'sip:[email protected]', 
  'password': 'password', 
  'ws_servers': 'wss://webrtc.registration.bandwidth.com:10443'
});
bwPhone.start();

bwPhone.on("registered", function(){
    bwPhone.call("222-333-4444", callOptions);    
});

bwPhone.on("newRTCSession", function(data){
    var session = data.session; // outgoing call session here
    session.on("confirmed",function(){
        //the call has connected, and audio is playing
    });
    session.on("ended",function(){
        //the call has ended
    });
    session.on("failed",function(){
        // unable to establish the call
    });
    
    //play a DTMF tone
    session.sendDTMF("1");
    session.sendDTMF("#");

    //mute call
    session.mute({audio: true});

    //unmute call
    session.unmute({audio: true});

    //to hangup the call
    session.terminate();

    
});

Inbound Call

var callOptions = {
  mediaConstraints: {
    audio: true, // only audio calls
    video: false
  }
};

var bwPhone = new JsSIP.UA({
  'uri': 'sip:[email protected]', 
  'password': 'password', 
  'ws_servers': 'wss://webrtc.registration.bandwidth.com:10443'
});
bwPhone.start();

bwPhone.on("newRTCSession", function(data){
    var session = data.session; 
    
    if (session.direction === "incoming") {
        // incoming call here
        session.on("accepted",function(){
            //the call has answered
        });
        session.on("ended",function(){
            //the call has ended
        });
        session.on("failed",function(){
            // unable to establish the call
        });
        
        // Answer call
        session.answer(callOptions);
        
        // Reject call (or hang up it)
        session.terminate();
    }
});

Passwordless connection (via auth token)

var authToken = "1234567890"; // you can get this token by POST /v1/users/<userId>/domains/<domainId>/endpoints/<endpointId>/tokens

var authHeader = "X-Callsign-Token: " + authToken;

var callOptions = {
  extraHeaders: [authHeader], // set auth token here (it will be passed on making calls and answering incoming call) 
  mediaConstraints: {
    audio: true, // only audio calls
    video: false
  }
};

var bwPhone = new JsSIP.UA({
  'uri': 'sip:[email protected]', 
  'ws_servers': 'wss://webrtc.registration.bandwidth.com:10443',
   'register': false // don't register automatically on start()
});
bwPhone.registrator().setExtraHeaders([authHeader]); // set auth header on register
bwPhone.start();
bwPhone.on('connected', function(){
    // connected to websocket
    bwPhone.register(); // connect to SIP server
});

bwPhone.on('registered', function(){
     // ready to make calls and receive incoming calls
     // making a call
     bwPhone.call("222-333-4444", callOptions);
});

bwPhone.on("newRTCSession", function(data){
    var session = data.session; 
    
    if (session.direction === "incoming") {
        // answer incoming call
        session.answer(callOptions);
    }
});

Supported Browsers

  • Firefox
  • Chrome
  • Opera
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment