Get started with jssip CDN

MIT licensed

JSSIP is a library for WebRTC signaling and peer-to-peer communication.

Tags:
  • SIP

Stable version

Copied!

How to start using jssip CDN


// Include the JSSIP library using the CDN link
const script = document.createElement('script');
script.src = 'https://cdn.cdnhub.io/jssip/3.10.1/jssip.min.js';
document.head.appendChild(script);

// Wait for the library to load
window.onJSSIPReady = () => {
  const localVideo = document.createElement('video');
  localVideo.autoplay = true;
  localVideo.muted = true;
  document.body.appendChild(localVideo);

  const remoteVideo = document.createElement('video');
  document.body.appendChild(remoteVideo);

  const localPeerConnection = new RTCPeerConnection();
  localPeerConnection.onicecandidate = (event) => {
    if (event.candidate) {
      console.log('Sending ice candidate:', event.candidate);
      remotePeerConnection.addIceCandidate(event.candidate);
    }
  };

  localPeerConnection.onaddstream = (event) => {
    localVideo.srcObject = event.stream;
  };

  const remotePeerConnection = new RTCPeerConnection();
  remotePeerConnection.onicecandidate = (event) => {
    if (event.candidate) {
      console.log('Sending ice candidate:', event.candidate);
      localPeerConnection.addIceCandidate(event.candidate);
    }
  };

  remotePeerConnection.onaddstream = (event) => {
    remoteVideo.srcObject = event.stream;
  };

  // Create offer and set local description
  localPeerConnection.createOffer()
    .then((offer) => {
      return localPeerConnection.setLocalDescription(offer);
    })
    .then(() => {
      // Set remote description from the provided remote description
      const remoteDescription = new RTCSessionDescription({ sdp: 'your_remote_sdp_here' });
      return remotePeerConnection.setRemoteDescription(remoteDescription);
    })
    .then(() => {
      // Create answer and set local description
      return remotePeerConnection.createAnswer();
    })
    .then((answer) => {
      return remotePeerConnection.setLocalDescription(answer);
    })
    .then(() => {
      // ICE candidates from remote peer
      localPeerConnection.onicecandidate = (event) => {
        if (event.candidate) {
          console.log('Received ice candidate:', event.candidate);
          remotePeerConnection.addIceCandidate(event.candidate);
        }
      };

      // Signaling server to exchange offers and answers
      // Replace this with your signaling server implementation
      const signalingServer = new WebSocket('wss://your_signaling_server_url');

      signalingServer.onopen = () => {
        console.log('Signaling server connected');
        signalingServer.send(JSON.stringify({
          type: 'offer',
          sdp: localPeerConnection.localDescription,
        }));
      };

      signalingServer.onmessage = (event) => {
        const message = JSON.parse(event.data);

        switch (message.type) {
          case 'answer':
            remotePeerConnection.setRemoteDescription(new RTCSessionDescription(message.sdp));
            break;

          case 'ice':
            remotePeerConnection.addIceCandidate(new RTCIceCandidate(message.candidate));
            break;
        }
      };

      signalingServer.onclose = () => {
        console.log('Signaling server disconnected');
      };
    })
    .catch((error) => {
      console.error('Error:', error);
    });
};

All versions