Skip to content

Instantly share code, notes, and snippets.

@haipyn
Forked from dtolb/jssip.md
Created August 24, 2018 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haipyn/3d54e6fd87e0808dbde1f23b72fc5eee to your computer and use it in GitHub Desktop.
Save haipyn/3d54e6fd87e0808dbde1f23b72fc5eee to your computer and use it in GitHub Desktop.
JsSip Demo

#JSSIP with Catapult API ​ ##Prerequisites ​

  • Register for a Catapult (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-minifiedJSFIDDLE Demo Client ​ ###Outbound Call
var callOptions = {
  mediaConstraints: {
    audio: true, // only audio calls
    video: false
  }
};var bwPhone = new JsSIP.UA({
  'uri': 'sip:sip-user@your-domain.bwapp.bwsip.io', 
  '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
    var dtmfSender;
    session.on("confirmed",function(){
        //the call has connected, and audio is playing
        var localStream = session.connection.getLocalStreams()[0];
        dtmfSender = session.connection.createDTMFSender(localStream.getAudioTracks()[0])
    });
    session.on("ended",function(){
        //the call has ended
    });
    session.on("failed",function(){
        // unable to establish the call
    });
    session.on('addstream', function(e){
        // set remote audio stream (to listen to remote audio)
        // remoteAudio is <audio> element on page
        remoteAudio.src = window.URL.createObjectURL(e.stream);
        remoteAudio.play();
    });
    
    //play a DTMF tone (session has method `sendDTMF` too but it doesn't work with Catapult server right)
    dtmfSender.insertDTMF("1");
    dtmfSender.insertDTMF("#");//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:sip-user@your-domain.bwapp.bwsip.io', 
  '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("confirmed",function(){
            // this handler will be called for incoming calls too
        });
        session.on("ended",function(){
            // the call has ended
        });
        session.on("failed",function(){
            // unable to establish the call
        });
        session.on('addstream', function(e){
            // set remote audio stream (to listen to remote audio)
            // remoteAudio is <audio> element on page
            remoteAudio.src = window.URL.createObjectURL(e.stream);
            remoteAudio.play();
        });
        
        // 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 http request to /v1/users/<userId>/domains/<domainId>/endpoints/<endpointId>/tokensvar 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:sip-user@your-domain.bwapp.bwsip.io', 
  'ws_servers': 'wss://webrtc.registration.bandwidth.com:10443',
});
bwPhone.registrator().setExtraHeaders([authHeader]); // set auth header on registerbwPhone.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);
    }
});bwPhone.start();

​ ​ ##Supported Browsers

  • Firefox
  • Chrome
  • Opera ​ You can check if browser supports WebRTC by code ​
    if (!JsSIP.rtcninja.hasWebRTC()) {
        alert("WebRTC is not supported. Please use another browser.");
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment