JsSIP Documentation

repository·master·Indexed 25 days ago

https://github.com/versatica/jssip

A 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.

Tokens
7.2K
Snippets
5
Records
57
Agent score
82%

What's inside JsSIP

  1. How JsSIP works

    master
    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.
  2. Build JsSIP from source

    master

    To 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 install
  3. Manage WebRTC media sessions with RTCSession

    master

    The RTCSession class 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(), or isEnded().

  4. Monitor UA status and connection events

    master

    The UA class is an EventEmitter. 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.
  5. Handle RTCSession events

    master

    RTCSession extends EventEmitter and 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. Provides originator, message, and cause.
    • failed: Fired when the session fails. Provides originator, message, and cause.
    • newDTMF: Fired when new DTMF tones are received. Provides dtmf object with tone and duration.
    • icecandidate: Fired when a new ICE candidate is available.
    • reinvite: Fired when a re-INVITE request is received. Provides a reject callback.
    • refer: Fired when a REFER request is received. Provides an accept callback.
    • sdp: Fired when SDP information is received.

    Event data types are mapped in RTCSessionEventMap.

  6. Make a SIP call with JsSIP

    master

    To make a SIP call, you need to initialize a JsSIP.WebSocketInterface, configure a JsSIP.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 provide eventHandlers to manage the lifecycle of the call (e.g., progress, failed, ended, confirmed) and mediaConstraints to 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);
  7. Configure Transport recovery options

    master

    When instantiating a Transport instance, you can provide a recovery_options object 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_INTERVAL and JsSIP_C.CONNECTION_RECOVERY_MAX_INTERVAL.

  8. Manage URI parameters

    master

    SIP 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 is null or undefined, it is stored as null. Values are converted to strings.
    • getParam(key): Returns the value of the specified parameter (case-insensitive key).
    • hasParam(key): Returns true if the parameter exists.
    • deleteParam(parameter): Removes the parameter and returns its value.
    • clearParams(): Removes all parameters.
  9. Initiate an outgoing call with connect()

    master

    To 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 for getUserMedia (e.g., { audio: true, video: true }).
      • mediaStream: An optional existing MediaStream to use.
      • pcConfig: Configuration for the RTCPeerConnection (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 the from URI.
      • fromDisplayName: Custom display name for the from header.
      • sessionTimersExpires: Custom expiration time for session timers.