- 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
Full docs are here CDN hosted library: minified not-minified JSFIDDLE Demo Client
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
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();
});
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("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();
}
});
var authToken = "1234567890"; // you can get this token by POST http request to /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',
});
bwPhone.registrator().setExtraHeaders([authHeader]); // set auth header on register
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);
}
});
bwPhone.start();
- 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.");
}
- Thank you, @GaborTorma for updating and answering questions.
You can hangup call with
session.terminate()