Case Study/Voice & Telephony/Design, engineering, maintenance

WebRTC Softphone

Author
Aryan
Client
Open source
Published
2026
Length
6 min · 1323 words

Abstract

Browser PSTN calling on Telnyx Call Control with WebRTC audio and live per-call cost metering.

Key outcomes

Network
PSTN + WebRTC
Edge
Telnyx Call Control
Billing
Per-call live meter
WebRTC Softphone — interface preview
Figure 1. WebRTC Softphone, Open source, 2026.

The pipeline is the interesting part

Placing a real phone call from a computer still meant choosing between two bad options. Buy a per-seat softphone product and inherit someone else’s idea of what a call is, or wire up a carrier SDK and spend a week on TwiML before the first dial tone. Both put the plumbing in front of the thing that actually mattered.

For voice-AI work the audio pipeline is the interesting part: who owns the call session, how state gets from the carrier to the UI, and whether the same path can serve a human agent or an AI agent without being rewritten. The softphone exists to solve that part once, properly, and then get out of the way.

Call control lives in the application

The single most consequential choice is that the application owns the call session, not the carrier.

With a hosted voice product the carrier decides the call flow, and it is merely configured. That works until behaviour is needed that the configuration model cannot express, at which point the platform is being fought instead of used. Here the carrier is reduced to what it is actually good at: terminating PSTN audio and delivering media. Everything above that, dialling, answering, bridging legs, recording decisions, hangup conditions, lives in application code, where it can be read, tested and changed.

Concretely: Telnyx Call Control is driven as a command API. The backend issues commands (call.dial, call.answer, call.bridge, call.hangup) and receives the carrier’s notifications as webhooks. The application is the state machine; the carrier is a transport that reports what the network did.

That inversion is what makes the rest of the design possible. Because there is no carrier-side flow to configure, the definition of “a call” is a data structure in my codebase, and a call can therefore be operated on by anything that can read it.

Two halves, one session

The system splits cleanly along the media boundary.

The browser holds the WebRTC leg. React 19 renders the interface and the WebRTC client owns the peer connection: device selection, local audio, ICE, and the remote track. Audio never touches the backend, which is the point. Routing media through a server would add latency, CPU cost and a failure mode for no benefit, so the browser negotiates directly with the carrier’s media edge.

The backend holds the session. A Bun and Express service owns the Telnyx API credentials, mints the client token the browser uses to reach the media edge, verifies inbound webhook signatures, and maintains the authoritative record of what is happening. It is deliberately stateless about audio and authoritative about everything else.

The seam between them is a small, explicit contract: the browser is told “dial this number as this call id”, and it reports back “the connection is up” or “the connection failed”. Neither side reaches into the other’s domain.

Every state transition is an event

The design rule that kept this maintainable is that the UI never asks what is happening. It is told.

Every meaningful transition, initiated, ringing, answered, bridged, held, ended, arrives as a webhook from the carrier and is written to the session record before it is pushed to any client. The front end subscribes and renders from that stream. There is no polling loop, no setInterval guessing at whether a call connected, and no possibility of the UI believing something the backend does not.

That choice pays off in three specific places:

  • Ordering. Carriers do not guarantee webhook delivery order, and a late ringing after an answered is entirely normal. Because each event carries the call id and its own timestamp, state transitions are applied as a guarded reducer rather than assigned, so a stale event cannot walk the UI backwards into a state the call already left.
  • Idempotency. Webhooks are retried, so every handler is keyed on the event id and a redelivery is a no-op. Without this, a retried hangup will happily fire a second time and double-count a call.
  • Recovery. Because the session record is the truth, a browser refresh mid-call can rehydrate the UI from the backend rather than losing the call’s history.

The state machine is small enough to read in one sitting, and that is not an accident. The carrier’s legal transitions are encoded explicitly, and an event that does not fit is logged rather than silently swallowed. Most voice bugs are state bugs, and the fastest way to find them is to refuse to accept impossible transitions.

Cost is a first-class output

Per-seat softphones hide the thing operators actually want to know: what did that call cost.

Metering is built into the session rather than bolted on afterwards. Each call carries its start and end, the carrier primitives it consumed, and the applicable rate, and the running total is surfaced in the UI while the call is still live. “How much did that pilot cost” stops being a billing surprise at the end of the month and becomes a number on screen during the call.

Two decisions make it defensible rather than decorative. First, duration is derived from the carrier’s own reported answer and hangup timestamps, not from a client-side timer, so the meter cannot drift from the invoice. Second, billing is computed from the session record, so a call that is resumed after a reconnect is still one call with one total.

The hard parts, honestly

WebRTC negotiation is the part nobody warns about. Producing a working call on a good network is straightforward. Producing one on a corporate network with a symmetric NAT is where the time goes: ICE candidate gathering, STUN and TURN configuration, and a fallback path that still works when direct connectivity is impossible. The interface has to communicate “the network is the problem” distinctly from “the call failed”, because those require completely different actions from the operator.

Permission and device state are user-visible failure modes. Microphone permission is denied, revoked, or bound to a device that was unplugged between calls. Treating device enumeration and permission state as first-class call state, rather than an error thrown inside the dial handler, is what stops the UI from rendering a call that cannot possibly carry audio.

Reconnection is a state, not an error. Losing the socket mid-call is normal on a laptop that changes networks. The call is not over, the UI is just blind for a moment, so the client reconnects, rehydrates from the session record and resumes rendering. Conflating “I lost the control channel” with “the call ended” is the mistake that turns a hiccup into a dropped call.

Why it matters beyond being a softphone

The softphone is the reference implementation underneath the voice agents I ship.

Once call control, media and metering are solved in the application, the human listening to the call becomes an implementation detail. The leg that answers can be a browser operator or a model-driven agent; the state machine does not care, because in both cases it is receiving events and issuing commands. Swapping the listener for an LLM becomes a change at the edges rather than a rewrite of the call path.

That is the actual return on this project: browser-to-PSTN calling became boring, and boring infrastructure is what voice products are built on top of.

What I would do differently

Two things.

The webhook handlers started out validating payload shape inline, and they should have been parsed into a typed event union at the boundary from the first commit. Retrofitting that later meant touching every handler, and it is the kind of change that only gets harder.

Second, cost metering was initially computed on read from the session record, which is correct but wasteful given how often the UI re-renders. Moving to an incrementally maintained total with the full calculation retained as the source for reconciliation kept the UI cheap without giving up the ability to recompute from first principles.

References

  1. [Source repository]github.com/Uo1428/virtual-phone
  2. [Author]Aryan (Uo1428)

Built with

React 19TypeScriptBunExpressWebRTCTelnyx

Related production systems from the same category.

All case studies →

Want this kind of system on your side of the table?