Getting a browser to place a real SIP call is a messy weekend. You need a JavaScript SIP stack, a WebSocket signalling path, WebRTC media (DTLS-SRTP, ICE, a codec), and something on the other end that answers. Most samples give you one of those pieces. I wanted a repo where docker compose up -d and npm run dev is enough to register, call an echo test, walk an IVR with DTMF, and call another browser tab.

That repo is webrtc-sip-softphone. This post is a walk through how it is put together, how the tests work, and what the lab deliberately leaves out.

What you get

The softphone is framework-free TypeScript and Vite. It registers over ws:// or wss://, dials a number or a SIP URI, answers, hangs up, mutes, holds and resumes, and sends DTMF (RFC 2833 in-media, or SIP INFO). During a call it shows live stats from RTCPeerConnection.getStats(): codec, packets, loss, jitter and RTT. One call at a time; a second incoming call is rejected with 486 Busy Here.

The lab is a docker-compose file:

  • Kamailio 5.8 as the WebSocket SIP proxy and in-memory registrar (RFC 7118 on port 8088), with NAT handling for WebSocket clients, record-routing, and routing of lab numbers to FreeSWITCH and of everything else to registered softphones.
  • FreeSWITCH 1.10 with a single-file config, WebRTC media (DTLS-SRTP, ICE, Opus) and three extensions:
Dial What happens
9196 Echo: you hear yourself, which tests the full media path
5000 IVR: a flite text-to-speech menu. 1 echo, 2 tone, 3 goodbye and hang up
9664 Continuous 440 Hz tone
1002 (and so on) Another softphone. Open a second tab with ?user=1002

FreeSWITCH runs in Kamailio's network namespace (network_mode: service:kamailio). Kamailio reaches it on loopback, the FreeSWITCH SIP port is not published, and only the RTP range is exposed.

Quickstart

You need Docker with Compose, Node 20+, a Chromium or Firefox browser, and a microphone.

docker compose up -d --wait npm install npm run dev # http://localhost:5173 

Click Connect (defaults: ws://localhost:8088, sip:1001@lab.local, no password) and dial 9196. For phone-to-phone, open http://localhost:5173/?user=1002&autoconnect in a second tab and call 1002.

If you want to call from another device on your LAN, start the lab with EXT_RTP_IP=<your LAN IP> and serve the page over HTTPS. Browsers only allow the microphone on https:// or localhost.

Architecture, in short

The UI talks to a Softphone class, which talks to a small UALike interface. The only production adapter is JsSIP. The phone's behaviour is a pure reducer in src/core/state.ts, driven by SIP events. That split is what makes the unit tests possible: everything except the JsSIP adapter is tested without a browser.

A call to the IVR looks like this:

  1. The browser REGISTERs over the WebSocket. Kamailio stores the contact with a WebSocket alias and returns 200.
  2. The phone INVITEs sip:5000@lab.local with an SDP offer (Opus, DTLS, ICE).
  3. Kamailio record-routes and forwards the INVITE to FreeSWITCH over UDP on loopback.
  4. FreeSWITCH answers with SDP. ICE and DTLS complete, then SRTP audio flows between the browser and FreeSWITCH.
  5. FreeSWITCH plays the menu. The phone sends DTMF 1 as RFC 2833 telephone-event. FreeSWITCH transfers that call into echo().
  6. BYE tears the call down along the record-route set, with Kamailio handling the WebSocket alias on the way back.

The README has mermaid diagrams for the architecture and this sequence if you want the boxed version.

Why the state is a reducer

SIP events arrive late, twice, or out of order. A hold request should be ignored if the call already ended. A second incoming INVITE should not clobber an active call. Putting that in event-handler closures gets messy, and it is hard to test.

A reducer ((state, event) => nextState) can be fed a recorded list of events in a unit test, including the late and out-of-order ones, with no WebSocket and no browser. The Softphone class is tested against a fake user agent for dialling options, mute/hold/DTMF and the busy rejection. Settings validation, dial-string normalisation, DTMF checks, duration formatting and stats summarising are all pure functions.

There are 42 of those unit tests, run with Vitest (npm test).

Real-call end-to-end tests

Unit tests cannot tell you that ICE connected or that FreeSWITCH actually switched into echo after a DTMF 1. For that there are 4 Playwright tests (npm run test:e2e) that drive Chromium against the running lab, with a fake microphone:

  1. Register, call 9196, wait until remote audio packets arrive, hang up.
  2. Call the IVR, press 1, and assert via fs_cli that FreeSWITCH is now running echo on that call.
  3. Two browser contexts register as 1001 and 1002, call each other through Kamailio, and hold/resume.
  4. Calling an unregistered number ends with Not Found.

GitHub Actions runs the unit job, then a job that starts the lab with docker compose up --wait, checks the Kamailio config, and runs the Playwright suite.

A limit of those tests is worth stating clearly: Chromium's fake microphone means the audio checks are packet counts, not a human listening. The suite has not been run against Safari, real handsets, TURN, or a path that crosses real NAT. The lab's media path assumes the browser and the Docker host can reach each other directly.

What the lab deliberately leaves out

This is a development lab, not a box you should put on the public internet as-is.

  • TLS. Use wss:// (Kamailio's tls module, or a TLS-terminating proxy in front of port 8088).
  • Auth. The lab registrar accepts anyone. Add digest auth (auth / auth_db) or token auth before exposing it.
  • Media relay. Behind real NAT, use TURN for browsers and/or rtpengine with Kamailio.
  • FreeSWITCH lock-down. The profile has auth-calls=false and is only safe because it listens on loopback.

I would rather document those gaps than pretend the compose file is production-ready.

If you want to try it

The code is MIT licensed: github.com/AhmedElbashier/webrtc-sip-softphone. Issues and reviews are welcome. I also published an ASP.NET Core 8 webhook relay for WhatsApp Cloud API and HubSpot in the same week, if that is more your stack: dotnet-webhook-relay.

More of my work is on my site.