JsSIP Documentation
repository·master·Indexed 25 days ago
https://github.com/versatica/jssipA lightweight SIP client for the browser and Node.js that uses WebSockets for transport and WebRTC for audio/video media. It supports audio/video calls, instant messaging, and is interoperable with SIP servers such as Kamailio, Asterisk, Mobicents, and reSIProcate. The library provides interfaces for managing User Agents (UA), RTCSessions, and SIP MESSAGE transactions.
What's inside JsSIP
- JsSIP is a lightweight SIP client that runs in both the browser and Node.js. It uses SIP over WebSockets as its transport mechanism. It supports audio/video calls via WebRTC and instant messaging. It is designed to be interoperable with SIP servers such as Kamailio, Asterisk, Mobicents, and reSIProcate.
Install JsSIP via npm
masterTo use JsSIP in your project, install it using npm:
$ npm install jssipRun JsSIP unit tests
masterExecute the project's unit tests using the following npm command:
npm run testRecompile JsSIP Grammar
masterIf you modify the grammar definition file located at
src/Grammar.pegjs, you must recompile it using the custom npm script to ensure the parser is updated.node npm-scripts.js grammarBuild JsSIP from source
masterTo build JsSIP from the source code, clone the repository, navigate to the directory, and install the necessary Node.js dependencies using npm.
git clone https://github.com/versatica/JsSIP.git JsSIP cd JsSIP npm installManage WebRTC media sessions with RTCSession
masterThe
RTCSessionclass is the primary interface for managing WebRTC-based SIP sessions, including calls and messaging. It handles the lifecycle of a session from initiation (outgoing) or reception (incoming) through to termination.Key session states include:
STATUS_NULL: Initial state.STATUS_INVITE_SENT/STATUS_INVITE_RECEIVED: Signaling phase.STATUS_WAITING_FOR_ANSWER: Waiting for the remote party to respond.STATUS_ANSWERED: The call is connected.STATUS_CONFIRMED: The session is fully established (ACK received).STATUS_CANCELED/STATUS_TERMINATED: The session has ended.
You can check the session state using
isInProgress(),isEstablished(), orisEnded().Monitor UA status and connection events
masterThe
UAclass is anEventEmitter. You can listen to several key events to monitor the lifecycle and connection state:connecting: Transport is attempting to connect.connected: Transport is successfully connected.disconnected: Transport has disconnected.registered: User is successfully registered.unregistered: User has been unregistered.registrationFailed: Registration attempt failed.newRTCSession: A new RTC session (call) has been created.newMessage: A new SIP MESSAGE has been received.newOptions: A new SIP OPTIONS request has been received.
Handle RTCSession events
masterRTCSessionextendsEventEmitterand supports a wide range of events via the.on()method. Common events include:accepted: Fired when the session is accepted (incoming or outgoing).ended: Fired when the session ends. Providesoriginator,message, andcause.failed: Fired when the session fails. Providesoriginator,message, andcause.newDTMF: Fired when new DTMF tones are received. Providesdtmfobject withtoneandduration.icecandidate: Fired when a new ICE candidate is available.reinvite: Fired when a re-INVITE request is received. Provides arejectcallback.refer: Fired when a REFER request is received. Provides anacceptcallback.sdp: Fired when SDP information is received.
Event data types are mapped in
RTCSessionEventMap.Make a SIP call with JsSIP
masterTo make a SIP call, you need to initialize a
JsSIP.WebSocketInterface, configure aJsSIP.UA(User Agent) instance with your credentials, and then call the.call()method on that instance.Note that you must call
ua.start()before attempting to make calls. You can provideeventHandlersto manage the lifecycle of the call (e.g.,progress,failed,ended,confirmed) andmediaConstraintsto request audio or video access.var socket = new JsSIP.WebSocketInterface('wss://sip.myhost.com'); var configuration = { sockets : [ socket ], uri : 'sip:alice@example.com', password : 'superpassword' }; var ua = new JsSIP.UA(configuration); ua.start(); // Register callbacks to desired call events var eventHandlers = { 'progress': function(e) { console.log('call is in progress'); }, 'failed': function(e) { console.log('call failed with cause: '+ e.data.cause); }, 'ended': function(e) { console.log('call ended with cause: '+ e.data.cause); }, 'confirmed': function(e) { console.log('call confirmed'); } }; var options = { eventHandlers, mediaConstraints: { 'audio': true, 'video': true } }; var session = ua.call('sip:bob@example.com', options);Configure Transport recovery options
masterWhen instantiating a
Transportinstance, you can provide arecovery_optionsobject to control the behavior of the automatic reconnection logic. This is useful for managing how frequently the transport attempts to reconnect after a disconnection.Supported keys:
min_interval: The minimum interval in seconds between recovery attempts.max_interval: The maximum interval in seconds between recovery attempts.
If no options are provided, the transport uses default values from
JsSIP_C.CONNECTION_RECOVERY_MIN_INTERVALandJsSIP_C.CONNECTION_RECOVERY_MAX_INTERVAL.Manage URI parameters
masterSIP URI parameters (the key-value pairs following a
;in a URI) can be manipulated using the following methods:setParam(key, value): Sets a parameter. If the value isnullorundefined, it is stored asnull. Values are converted to strings.getParam(key): Returns the value of the specified parameter (case-insensitive key).hasParam(key): Returnstrueif the parameter exists.deleteParam(parameter): Removes the parameter and returns its value.clearParams(): Removes all parameters.
Initiate an outgoing call with connect()
masterTo start an outgoing WebRTC session, use the
connect(target, options)method.Parameters:
target: The SIP URI or target string to call.options: An object containing:eventHandlers: An object mapping session events (e.g.,progress,accepted,failed) to callback functions.mediaConstraints: Constraints forgetUserMedia(e.g.,{ audio: true, video: true }).mediaStream: An optional existingMediaStreamto use.pcConfig: Configuration for theRTCPeerConnection(e.g.,iceServers).rtcOfferConstraints: Constraints for the SDP offer.data: Custom data to attach to the session instance.anonymous: Boolean to indicate if the call should be made anonymously.fromUserName: Custom username for thefromURI.fromDisplayName: Custom display name for thefromheader.sessionTimersExpires: Custom expiration time for session timers.