npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

msg91-webrtc-call

v2.0.2

Published

**msg91-webrtc-call** is a lightweight JavaScript SDK that enables you to easily add peer-to-peer WebRTC audio/video calling functionality to your web applications using the MSG91 infrastructure.

Readme

📞 msg91-webrtc-call

msg91-webrtc-call is a lightweight JavaScript SDK that enables you to easily add peer-to-peer WebRTC audio/video calling functionality to your web applications using the MSG91 infrastructure.

📦 Installation

Install the package using npm or yarn:

npm install msg91-webrtc-call
# or
yarn add msg91-webrtc-call

🚀 Getting Started

1. Initialization

Initialize the WebRTC instance with your user token. You can optionally specify the environment ('prod', 'test', 'dev').

import WebRTC from "msg91-webrtc-call";

// Initialize with your user token
const userToken = "YOUR_USER_TOKEN";
const webrtc = WebRTC(userToken, "prod"); // Defaults to "prod"

2. Initiate a Call

To start a new call, you need a valid call token.

const callToken = "YOUR_CALL_TOKEN";
await webrtc.call(callToken);

3. Rejoin an Ongoing Call

If a user gets disconnected, they can rejoin the call using the call ID.

await webrtc.rejoinCall("existing_call_id");

4. Send User Context

Send contextual information about the user to the backend.

webrtc.sendUserContext({
  name: "User Name",
  email: "[email protected]",
  customData: "some-value"
});

5. Global Ringtone Setting

webrtc.silence(true);   // Stop sending PLAY_RINGTONE event
webrtc.silence(false);  // Start sending PLAY_RINGTONE event

🎧 Handling Calls & Events

The WebRTC instance emits events that you should listen to for handling call flows.

Listening for Events

import { WebRTC_EVENT } from "msg91-webrtc-call";

// Handle Incoming Calls
webrtc.on(WebRTC_EVENT.INCOMING_CALL, (incomingCall) => {
    console.log("Incoming call from:", incomingCall.getInfo().from);
    
    // Handle UI for incoming call
    incomingCall.accept(); // or incomingCall.reject();
});

// Handle Outgoing Calls
webrtc.on(WebRTC_EVENT.OUTGOING_CALL, (outgoingCall) => {
    console.log("Dialing...");
});

// Ringtone Management
webrtc.on(WebRTC_EVENT.PLAY_RINGTONE, () => {
    // Play your ringtone audio
    audioElement.play();
});

webrtc.on(WebRTC_EVENT.STOP_RINGTONE, () => {
    // Stop your ringtone audio
    audioElement.pause();
    audioElement.currentTime = 0;
});

📘 Call Object API

Both IncomingCall and OutgoingCall share common methods and events.

Common Methods

  • getInfo(): Returns CallInfo containing id, from, to, type.
  • mute(): Mutes the user's audio.
  • unmute(): Unmutes the user's audio.
  • hang(): Ends the call.
  • getMediaStream(): Returns the MediaStream object.
  • getStatus(): Returns current status (idle, ringing, connected, ended).

IncomingCall Specifics

  • accept(): Answers the call.
  • reject(): Rejects the call.
  • silence(status: boolean): Silences the ringtone for this specific call.

OutgoingCall Specifics

  • cancel(): Cancels the call before it is answered.
  • sendMessage(message: MessageArray): Sends a message (e.g., to a bot).

Call Events

Listen to these events on the call object (both incoming and outgoing).

import { CALL_EVENT } from "msg91-webrtc-call";

call.on(CALL_EVENT.CONNECTED, (mediaStream) => {
    // Call connected, attach mediaStream to an audio element
    const audioRef = document.getElementById("audio-element");
    audioRef.srcObject = mediaStream;
    audioRef.play();
});

call.on(CALL_EVENT.ENDED, (payload) => {
    console.log("Call ended", payload);
});

call.on(CALL_EVENT.ANSWERED, (payload) => {
    console.log("Call answered by", payload.answeredBy);
});

call.on(CALL_EVENT.ERROR, (error) => {
    console.error("Call error", error);
});

// For incoming calls
call.on(CALL_EVENT.UNAVAILABLE, () => {
    console.log("Call picked up by another device or cancelled");
});

⚛️ React Integration Example

import { useEffect, useState, useRef } from "react";
import WebRTC, { WebRTC_EVENT, CALL_EVENT } from "msg91-webrtc-call";

const CallComponent = ({ userToken }) => {
  const [call, setCall] = useState(null);
  const webrtcRef = useRef(null);

  useEffect(() => {
    if (!userToken) return;

    const webrtc = WebRTC(userToken);
    webrtcRef.current = webrtc;

    webrtc.on(WebRTC_EVENT.INCOMING_CALL, (incomingCall) => {
      setCall(incomingCall);
    });
    
    // Cleanup
    return () => {
        // The WebRTC instance is a singleton. 
        // Calling webrtc.close() would disconnect the socket for the entire session.
        // Only close if you are sure the user is logging out or leaving the app.
        // webrtc.close(); 
    };
  }, [userToken]);

  const handleAccept = () => {
    if (call) {
        call.accept();
        call.on(CALL_EVENT.CONNECTED, (stream) => {
            // Attach stream to audio element
        });
    }
  };

  return (
    <div>
      {call && <button onClick={handleAccept}>Accept Call</button>}
    </div>
  );
};

🔧 Types & Enums

The library exports several useful types and enums:

  • WebRTC_EVENT: Events emitted by the main WebRTC instance.
  • CALL_EVENT: Events emitted by Call instances.
  • CALL_STATUS: Status of the call (idle, ringing, connected, ended).
  • CALL_TYPE: Type of call (incoming-call, outgoing-call).
import { WebRTC_EVENT, CALL_EVENT, CALL_STATUS } from "msg91-webrtc-call";