<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Elbashier</title>
    <description>The latest articles on DEV Community by Ahmed Elbashier (@ahmedhex).</description>
    <link>https://dev.to/ahmedhex</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F644169%2Fad843cf6-de21-4f53-982c-8abd7e327b9e.jpg</url>
      <title>DEV Community: Ahmed Elbashier</title>
      <link>https://dev.to/ahmedhex</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedhex"/>
    <language>en</language>
    <item>
      <title>How I built a browser softphone with a one-command Kamailio + FreeSWITCH lab and real-call E2E tests</title>
      <dc:creator>Ahmed Elbashier</dc:creator>
      <pubDate>Wed, 25 Nov 2026 07:53:01 +0000</pubDate>
      <link>https://dev.to/ahmedhex/how-i-built-a-browser-softphone-with-a-one-command-kamailio-freeswitch-lab-and-real-call-e2e-tests-33h0</link>
      <guid>https://dev.to/ahmedhex/how-i-built-a-browser-softphone-with-a-one-command-kamailio-freeswitch-lab-and-real-call-e2e-tests-33h0</guid>
      <description>&lt;p&gt;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 &lt;code&gt;docker compose up -d&lt;/code&gt; and &lt;code&gt;npm run dev&lt;/code&gt; is enough to register, call an echo test, walk an IVR with DTMF, and call another browser tab.&lt;/p&gt;

&lt;p&gt;That repo is &lt;a href="https://github.com/AhmedElbashier/webrtc-sip-softphone" rel="noopener noreferrer"&gt;webrtc-sip-softphone&lt;/a&gt;. This post is a walk through how it is put together, how the tests work, and what the lab deliberately leaves out.&lt;/p&gt;

&lt;h2&gt;
  
  
  What you get
&lt;/h2&gt;

&lt;p&gt;The softphone is framework-free TypeScript and Vite. It registers over &lt;code&gt;ws://&lt;/code&gt; or &lt;code&gt;wss://&lt;/code&gt;, 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 &lt;code&gt;RTCPeerConnection.getStats()&lt;/code&gt;: codec, packets, loss, jitter and RTT. One call at a time; a second incoming call is rejected with &lt;code&gt;486 Busy Here&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The lab is a &lt;code&gt;docker-compose&lt;/code&gt; file:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Kamailio 5.8&lt;/strong&gt; 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.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;FreeSWITCH 1.10&lt;/strong&gt; with a single-file config, WebRTC media (DTLS-SRTP, ICE, Opus) and three extensions:&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dial&lt;/th&gt;
&lt;th&gt;What happens&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;9196&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Echo: you hear yourself, which tests the full media path&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;5000&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;IVR: a flite text-to-speech menu. &lt;strong&gt;1&lt;/strong&gt; echo, &lt;strong&gt;2&lt;/strong&gt; tone, &lt;strong&gt;3&lt;/strong&gt; goodbye and hang up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;9664&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Continuous 440 Hz tone&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;1002&lt;/code&gt; (and so on)&lt;/td&gt;
&lt;td&gt;Another softphone. Open a second tab with &lt;code&gt;?user=1002&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;h2&gt;
  
  
  Quickstart
&lt;/h2&gt;

&lt;p&gt;You need Docker with Compose, Node 20+, a Chromium or Firefox browser, and a microphone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;docker compose up -d --wait
npm install
npm run dev   &lt;span class="c"&gt;# http://localhost:5173&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;If you want to call from another device on your LAN, start the lab with &lt;code&gt;EXT_RTP_IP=&amp;lt;your LAN IP&amp;gt;&lt;/code&gt; and serve the page over HTTPS. Browsers only allow the microphone on &lt;code&gt;https://&lt;/code&gt; or &lt;code&gt;localhost&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Architecture, in short
&lt;/h2&gt;

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

&lt;p&gt;A call to the IVR looks like this:&lt;/p&gt;

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

&lt;p&gt;The README has mermaid diagrams for the architecture and this sequence if you want the boxed version.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the state is a reducer
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;A reducer (&lt;code&gt;(state, event) =&amp;gt; nextState&lt;/code&gt;) 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 &lt;code&gt;Softphone&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;There are 42 of those unit tests, run with Vitest (&lt;code&gt;npm test&lt;/code&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Real-call end-to-end tests
&lt;/h2&gt;

&lt;p&gt;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 (&lt;code&gt;npm run test:e2e&lt;/code&gt;) that drive Chromium against the running lab, with a fake microphone:&lt;/p&gt;

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

&lt;p&gt;GitHub Actions runs the unit job, then a job that starts the lab with &lt;code&gt;docker compose up --wait&lt;/code&gt;, checks the Kamailio config, and runs the Playwright suite.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the lab deliberately leaves out
&lt;/h2&gt;

&lt;p&gt;This is a development lab, not a box you should put on the public internet as-is.&lt;/p&gt;

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

&lt;p&gt;I would rather document those gaps than pretend the compose file is production-ready.&lt;/p&gt;

&lt;h2&gt;
  
  
  If you want to try it
&lt;/h2&gt;

&lt;p&gt;The code is MIT licensed: &lt;a href="https://github.com/AhmedElbashier/webrtc-sip-softphone" rel="noopener noreferrer"&gt;github.com/AhmedElbashier/webrtc-sip-softphone&lt;/a&gt;. 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: &lt;a href="https://github.com/AhmedElbashier/dotnet-webhook-relay" rel="noopener noreferrer"&gt;dotnet-webhook-relay&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;More of my work is on &lt;a href="https://ahmedelbashier-portfolio.netlify.app/" rel="noopener noreferrer"&gt;my site&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webrtc</category>
      <category>voip</category>
      <category>typescript</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
