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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@2chat/voice-sdk

v0.2.1

Published

2Chat Voice SDK — embeddable, framework-agnostic browser voice calling.

Downloads

388

Readme

@2chat/voice-sdk

Embeddable, framework-agnostic browser voice calling for the 2Chat platform. JsSIP under the hood; a Device / Call public API on top.

  • Outbound + inbound calls
  • Mute, hold, DTMF
  • Short-lived JWT auth — your long-lived X-User-API-Key never reaches the browser
  • ESM, CJS, and UMD/CDN builds; zero React (or any framework) dependency

Install

npm install @2chat/voice-sdk

Or drop the UMD build directly into a <script> tag:

<script src="https://cdn.jsdelivr.net/npm/@2chat/voice-sdk/dist/index.global.js"></script>
<script>
  const { Device } = TwoChatVoice;
</script>

How auth works

  1. Your backend holds a long-lived 2Chat X-User-API-Key.
  2. Your backend calls POST /open/sdk/access-token with a user_uuid (and optional label) to mint a short-lived JWT.
  3. You hand that JWT to the browser; the SDK uses it to fetch SIP credentials and register.

The JWT is scoped to one 2Chat User — running N concurrent agents means minting N tokens, one per real User your backend has provisioned.


Quick start

import { Device } from "@2chat/voice-sdk";

// token comes from your backend: POST /open/sdk/access-token
const device = new Device({
  token: myJwt,
  logLevel: "info",
});

device.on("registered", () => console.log("ready"));
device.on("incoming", (call) => {
  if (confirm("Incoming call — accept?")) call.accept();
  else call.reject();
});
device.on("tokenWillExpire", async () => {
  const fresh = await fetch("/my/backend/voice-token").then((r) => r.text());
  await device.updateToken(fresh);
});
device.on("error", (err) => console.warn(err.code, err.message));

await device.register();

const call = await device.connect({
  to: "+15551234567",
  from: "+15550000000",          // caller ID
});

call.on("ringing",    () => {});
call.on("accepted",   () => {});
call.on("disconnect", ({ reason }) => console.log("ended:", reason));

call.mute(true);
call.hold(true);
call.sendDigits("1");
await call.disconnect();

API

new Device(options)

| option | type | default | notes | |---|---|---|---| | token | string | — | Required. JWT minted by your backend. | | logLevel | silent \| error \| warn \| info \| debug | warn | | | allowIncomingWhileBusy | boolean | false | If false, extra incoming calls get rejected with 486. | | iceServers | RTCIceServer[] | from credentials | Override ICE servers entirely. | | media | { inputDeviceId?, outputDeviceId? } | — | Initial device preferences. | | ringtoneUrl | string | — | Looped ringtone for inbound calls. | | tokenExpiryLeadMs | number | 60000 | How far before exp to fire tokenWillExpire. | | defaultExtraHeaders | string[] | — | Appended to every outbound call. |

Methods

  • register(): Promise<void> — fetch SIP creds, start the UA, send the initial REGISTER.
  • connect(params): Promise<Call> — place an outbound call.
  • updateToken(jwt): Promise<void> — swap the token in place (same user_uuid only).
  • unregister(): Promise<void> — send un-REGISTER and close the WS.
  • destroy(): void — unrecoverable teardown; call on page unload.
  • enumerateDevices(), setInputDevice(id), setOutputDevice(id)
  • isRegistered(): boolean, identity: TokenClaims, label?: string, tokenExpiresAt: Date

Events

| name | payload | |---|---| | registered | void | | unregistered | { reason?: string } | | incoming | Call | | tokenWillExpire | { expiresAt: Date } | | reconnecting | { attempt: number } | | reconnected | void | | error | VoiceSDKError |

Call

call.accept();            // inbound only
call.reject();            // inbound only
call.disconnect();
call.mute(true);
call.hold(true);
call.sendDigits("1");
call.setInputDevice(id);  // applies to this call only
call.setOutputDevice(id); // applies to this call only

call.direction   // 'inbound' | 'outbound'
call.getStatus() // 'pending' | 'ringing' | 'accepted' | 'disconnected' | 'failed'
call.isMuted()
call.isOnHold()
call.session     // raw JsSIP RTCSession escape hatch

Events: ringing, accepted, disconnect, error, mute, hold, quality.

Errors

All SDK errors are instances of VoiceSDKError with a typed .code:

import { VoiceSDKError } from "@2chat/voice-sdk";

device.on("error", (err: VoiceSDKError) => {
  switch (err.code) {
    case "TOKEN_EXPIRED":
    case "AUTH_ERROR":
      // re-mint the token
      break;
    case "REGISTRATION_FAILED":
      // let the user know
      break;
  }
});

Development

npm install
npm run typecheck
npm test
npm run build

# Try the vanilla example:
npm run build && npx serve .
# then navigate to examples/vanilla-html

License

MIT