<?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: Nay Htet Khant</title>
    <description>The latest articles on DEV Community by Nay Htet Khant (@nayhtetkhant).</description>
    <link>https://dev.to/nayhtetkhant</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%2F4239119%2F4d00d4b6-6127-4450-8f21-ef7249ef0a0b.jpg</url>
      <title>DEV Community: Nay Htet Khant</title>
      <link>https://dev.to/nayhtetkhant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nayhtetkhant"/>
    <language>en</language>
    <item>
      <title>Why P2P WebRTC failed at 5 users, and how I built an SFU video + chat app with LiveKit, Laravel Reverb, and Next.js [Open Source]</title>
      <dc:creator>Nay Htet Khant</dc:creator>
      <pubDate>Wed, 11 Nov 2026 13:12:28 +0000</pubDate>
      <link>https://dev.to/nayhtetkhant/why-p2p-webrtc-failed-at-5-users-and-how-i-built-an-sfu-video-chat-app-with-livekit-laravel-1lpj</link>
      <guid>https://dev.to/nayhtetkhant/why-p2p-webrtc-failed-at-5-users-and-how-i-built-an-sfu-video-chat-app-with-livekit-laravel-1lpj</guid>
      <description>&lt;p&gt;Hey everyone!&lt;/p&gt;

&lt;p&gt;I'm a 20-year-old self-taught developer from Thailand preparing to break into the industry as a junior full-stack/backend dev.&lt;/p&gt;

&lt;p&gt;I recently built an open-source group video conferencing and real-time chat app called Chatn. When I started, I thought I could just follow a basic WebRTC tutorial. But as soon as I tried scaling beyond 2 users on localhost, things broke down fast.&lt;/p&gt;

&lt;p&gt;Here are the main technical hurdles I ran into and how I solved them:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The P2P WebRTC Bottleneck (Switching to LiveKit SFU)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;My first prototype used a standard WebRTC P2P mesh. It worked fine between 2 browser tabs. But with 4–5 participants, my laptop fan went crazy and video froze.&lt;/p&gt;

&lt;p&gt;In P2P mesh, connections scale as O(N²). For 5 participants, every client has to encode and upload their video stream to 4 other peers (O(N) upload bandwidth). On typical home Wi-Fi with low upload bandwidth, the uplink gets choked immediately.&lt;/p&gt;

&lt;p&gt;The Fix: I switched to an SFU (Selective Forwarding Unit) using LiveKit. With an SFU, each client encodes and uploads only 1 stream to the server (O(1) upload). The server then forwards the video packets to the other peers. Client CPU usage dropped, and video stayed smooth even with multiple users.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Separating Video from Persistent Chat (Laravel Reverb)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;LiveKit has built-in data channels for text messaging, but WebRTC data channels are ephemeral. If someone joins the meeting 10 minutes late, they won't see any previous messages.&lt;/p&gt;

&lt;p&gt;I wanted real message persistence, authorization policies, and chat history. So I separated concerns:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;LiveKit: Dedicated strictly to heavy UDP video/audio packet routing.

Laravel Reverb (WebSockets) + MySQL: Handles real-time chat, authentication, DB transactions, and presence events.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;The "Shifting Window" Bug (Cursor Pagination)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For chat history, I initially used traditional offset pagination (LIMIT 20 OFFSET 20).&lt;/p&gt;

&lt;p&gt;In an active room, if someone is scrolling up through old messages while 5 new messages are being sent, the offset shifts. The user ends up seeing duplicate messages or skipping messages entirely.&lt;/p&gt;

&lt;p&gt;The Fix: Switched to keyset / cursor pagination (WHERE id &amp;lt; :last_seen_id ORDER BY id DESC LIMIT 20). Because the query is anchored to a specific message ID, new incoming messages at the top never alter the pagination window. It also does an instant primary key B-Tree index seek instead of scanning and discarding thousands of rows in MySQL.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Testing WebRTC on Mobile over LAN (Docker + Caddy + nip.io)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When I tried testing video calls between my laptop and physical phone on my Wi-Fi (&lt;a href="http://192.168.1.x" rel="noopener noreferrer"&gt;http://192.168.1.x&lt;/a&gt;), mobile browsers immediately blocked getUserMedia(). Browsers require a Secure Context (HTTPS) for camera and microphone permissions.&lt;/p&gt;

&lt;p&gt;The Fix:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Containerized the entire stack with Docker Compose.

Put Caddy in front as a reverse proxy.

Used nip.io wildcard DNS (https://192-168-1-50.nip.io).

Caddy automatically generates and trusts local SSL certificates, allowing my phone to access the app over HTTPS on the local network with zero certificate friction.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Tech Stack&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Backend API: PHP 8.2, Laravel 11, Laravel Reverb (WebSockets), MySQL

Media Server: LiveKit (SFU)

Frontend: Next.js 14 (App Router), TypeScript, Tailwind CSS, TanStack Query, Zustand

DevOps: Docker, Docker Compose, Caddy Reverse Proxy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;GitHub Repositories (Open Source)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Orchestration: https://github.com/nayhtetkhant/chatn-main

Backend API: https://github.com/nayhtetkhant/chatn-api

Frontend: https://github.com/nayhtetkhant/chatn-web

Portfolio: https://nayhtetkhant.com/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;I would love to get your thoughts, constructive feedback, or any potential architectural bottlenecks you spot in the repos! &lt;/p&gt;

</description>
      <category>devops</category>
      <category>opensource</category>
      <category>architecture</category>
      <category>php</category>
    </item>
  </channel>
</rss>
