Getting Started
JsSIP User Agent is the core element in JsSIP. It represents the SIP client associated to a SIP account. JsSIP User Agent is defined in JsSIP.UA
class.
Multiple JsSIP User Agents can be created (this is useful for having different SIP accounts running in the same web application).
Creating a JsSIP User Agent
User Agent Configuration
JsSIP User Agent requires a configuration object for its initialization. There are some mandatory configuration parameters and many optional ones. Check the full configuration parameters list.
var configuration = {
'ws_servers': 'ws://sip-ws.example.com',
'uri': 'sip:alice@example.com',
'password': 'superpassword'
};
User Agent instance
var coolPhone = new JsSIP.UA(configuration);
User Agent events definition
Full list of User Agent events.
WebSocket connection events
coolPhone.on('connected', function(e){ /* Your code here */ });
coolPhone.on('disconnected', function(e){ /* Your code here */ });
New incoming or outgoing call event
coolPhone.on('newRTCSession', function(e){ /* Your code here */ });
New incoming or outgoing IM message event
coolPhone.on('newMessage', function(e){ /* Your code here */ });
SIP registration events
coolPhone.on('registered', function(e){ /* Your code here */ });
coolPhone.on('unregistered', function(e){ /* Your code here */ });
coolPhone.on('registrationFailed', function(e){ /* Your code here */ });
Starting the User Agent
See JsSIP.UA.start() method definition.
coolPhone.start();
Making outbound calls
See JsSIP.UA.call() method definition.
Example
// HTML5 <video> elements in which local and remote video will be shown
var selfView = document.getElementById('my-video');
var remoteView = document.getElementById('peer-video');
// Register callbacks to desired call events
var eventHandlers = {
'progress': function(e){ /* Your code here */ },
'failed': function(e){ /* Your code here */ },
'confirmed': function(e){
var rtcSession = e.sender;
// Attach local stream to selfView
if (rtcSession.getLocalStreams().length > 0) {
selfView.src = window.URL.createObjectURL(rtcSession.getLocalStreams()[0]);
}
// Attach remote stream to remoteView
if (rtcSession.getRemoteStreams().length > 0) {
remoteView.src = window.URL.createObjectURL(rtcSession.getRemoteStreams()[0]);
}
},
'ended': function(e){ /* Your code here */ }
};
var options = {
'eventHandlers': eventHandlers,
'extraHeaders': [ 'X-Foo: foo', 'X-Bar: bar' ],
'mediaConstraints': {'audio': true, 'video': true}
};
coolPhone.call('sip:bob@example.com', options);
Instant messaging
See JsSIP.UA.sendMessage() method definition.
Example 1
var text = 'Hello Bob!';
coolPhone.sendMessage('sip:bob@example.com', text);
Example 2
var text = 'Hello Bob!';
// Register callbacks to desired message events
var eventHandlers = {
'succeeded': function(e){ /* Your code here */ },
'failed': function(e){ /* Your code here */ };
};
var options = {
'eventHandlers': eventHandlers
};
coolPhone.sendMessage('sip:bob@example.com', text, options);