<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Arpit Kapadia on Medium]]></title>
        <description><![CDATA[Stories by Arpit Kapadia on Medium]]></description>
        <link>https://medium.com/@arpitkapadia?source=rss-17d1af4dc074------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*3Z91iT97y6aGcnns.</url>
            <title>Stories by Arpit Kapadia on Medium</title>
            <link>https://medium.com/@arpitkapadia?source=rss-17d1af4dc074------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 00:05:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arpitkapadia/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Running a Web App Inside React Native Without Losing Your Mind]]></title>
            <link>https://medium.com/@arpitkapadia/running-a-web-app-inside-react-native-without-losing-your-mind-0eb143aabee5?source=rss-17d1af4dc074------2</link>
            <guid isPermaLink="false">https://medium.com/p/0eb143aabee5</guid>
            <category><![CDATA[web-for-app]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[cross-platform]]></category>
            <category><![CDATA[react-native-webview]]></category>
            <dc:creator><![CDATA[Arpit Kapadia]]></dc:creator>
            <pubDate>Thu, 23 Apr 2026 18:53:36 GMT</pubDate>
            <atom:updated>2026-04-23T19:38:28.646Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e8leogmaMXIqbdH2z6HagQ.png" /></figure><h4>An event-driven bridge that turned two drifting apps into one system</h4><p>Maintaining feature parity across web and mobile is one of those problems that looks clean on a whiteboard and slowly rots in production.</p><p>You start with:</p><ul><li>a React Native app that handles realtime, notifications, device access</li><li>a web app that handles complexity, iteration speed, and evolving workflows</li></ul><p>And then you wake up one day with:</p><ul><li>duplicated features</li><li>inconsistent behavior</li><li>two teams solving the same problem differently</li></ul><p>We hit that wall.</p><p>Instead of forcing React Native to catch up with web, we flipped the model:</p><blockquote><strong><em>We turned React Native into a host runtime, and the web app into a feature engine running inside it.</em></strong></blockquote><p>Not a wrapper. Not a hack.</p><p>A <strong>controlled, event-driven system</strong>.</p><h3>🧠 The Pattern: Dual Runtime System (DRS)</h3><p>This wasn’t “using a WebView”.<br>We treated it as a system with clear boundaries:</p><pre>+---------------------+<br>|   Native Runtime    |<br>|---------------------|<br>| Auth               |<br>| Navigation         |<br>| Native APIs        |<br>| Lifecycle          |<br>+----------+----------+<br>           |<br>           |  (Bridge Protocol)<br>           |<br>+----------v----------+<br>|     Web Runtime     |<br>|---------------------|<br>| UI Rendering        |<br>| Business Flows      |<br>| Fast Iteration      |<br>+---------------------+</pre><p>Two runtimes.<br>Each with a clear responsibility. Connected through a <strong>protocol</strong>, not direct calls.</p><p>We internally treated this like a system, not a workaround.</p><ul><li><strong>Native Shell</strong> → owns lifecycle, navigation, capabilities</li><li><strong>Web Engine</strong> → renders complex product logic</li><li><strong>Bridge Protocol</strong> → the contract between them</li></ul><p>If you want a name for it —<strong><em>“Dual Runtime System”</em></strong></p><h3>🔌 The Bridge Protocol (aka: don’t let chaos leak through)</h3><p>At the center of everything was what we called internally something closer to a <strong>Command Bus</strong> than a bridge.</p><p>Not:</p><blockquote><em>“</em>call this native function<em>”</em></blockquote><p>But:</p><blockquote><em>“</em><strong>send an intent, let the other side decide</strong><em>”</em></blockquote><h4>Core Idea: Command Bus</h4><p>Instead of tightly coupling web → native functions, we built a <strong>Command Bus</strong>.</p><pre>window.appPleaseDo = function(type, payload = {}) {<br>  window.ReactNativeWebView.postMessage(<br>    JSON.stringify({<br>      type,<br>      payload,<br>      requestId: Date.now()<br>    })<br>  )<br>}</pre><p>But no one uses this directly. We exposed a Command Layer via proper SDK</p><pre>export function closeApp() {<br>  dispatch(&quot;CLOSE_WEBVIEW&quot;)<br>}<br><br>export function startVideoCall(consultId: string) {<br>  dispatch(&quot;START_VIDEO_CALL&quot;, { consultId })<br>}</pre><p>From a feature developer’s POV:</p><pre>import { closeApp, startVideoCall } from @web-for-app // internally think: &quot;web runtime SDK&quot;)<br><br>closeApp()<br>triggerConsultCall(&quot;abc123&quot;)</pre><p>Feels native. Isn’t native.</p><h4>Why this matters (not obvious until you scale)</h4><p>This abstraction did 3 things:</p><p>1. <strong>Killed cognitive overhead<br> </strong>Frontend devs didn’t need to understand RN internals, WebView quirks, or serialization.</p><p>2. <strong>Prevented API drift<br> </strong>No one was manually crafting event payloads. The SDK enforced shape.</p><p>3. <strong>Made behavior swappable<br></strong>“Start video call” could be:</p><p>Without this layer:</p><ul><li>every dev constructs payloads differently</li><li>event names drift</li><li>debugging becomes guesswork</li></ul><p>With it:</p><ul><li>communication becomes predictable</li><li>contracts are implicitly enforced</li><li>behavior becomes swappable</li></ul><p>Example:</p><pre>&quot;START_VIDEO_CALL&quot;<br>        |<br>        v<br>RN decides:<br>- open native call<br>- or fallback to web<br>- or block based on config</pre><p>Same command. Different outcomes. That flexibility is where this pattern pays off.</p><h4>End-to-End Flow (how things actually move)</h4><p>Let’s take a real example: starting a video consult.</p><pre>[ Web UI ]<br>   |<br>   | dispatch(&quot;START_VIDEO_CALL&quot;, { consultId })<br>   v<br>[ Bridge Protocol ]<br>   |<br>   v<br>[ React Native ]<br>   |<br>   | decides:<br>   | -&gt; use native SDK<br>   v<br>[ Native Video Call Opens ]</pre><p>The web never “calls native”. It expresses intent. Native interprets and executes.</p><h3>🧬 Runtime Awareness (web needs to know where it lives)</h3><p>The web app had to shape-shift depending on where it live. The same web app runs in:</p><ul><li>browser</li><li>React Native WebView</li><li>sometimes iframe</li></ul><p>it <strong>must adapt</strong></p><h4>Detection Layer</h4><p>We built a small but critical mechanism to detect environment/host using:</p><ul><li>user-agent override → &quot;react-native&quot;</li><li>injected globals</li><li>handshake (ping-pong)</li></ul><pre>Step 1: Web loads<br>Step 2: Web sends &quot;PING&quot;<br>Step 3: RN injects flag<br>Step 4: Web confirms environment</pre><pre>window.appPleaseDo(&quot;PING&quot;)</pre><p>RN injects:</p><pre>window.__NATIVE_RUNTIME__ = true</pre><p>Then:</p><pre>export function isNativeRuntime() {<br>  return window.__NATIVE_RUNTIME__ === true<br>}</pre><h4>Why this matters</h4><p>This single signal controlled:</p><ul><li>analytics routing</li><li>auth strategy</li><li>UI composition (hide web header, use native header)</li><li>navigation behavior</li></ul><p>Without this, you end up with:</p><blockquote><em>“same app, different bugs everywhere”</em></blockquote><h3>🔐 Auth Flow (the most fragile part, handled deliberately)</h3><p>Auth is where most hybrid systems quietly break. We made one rule</p><blockquote><strong><em>Web never owns authentication when rendered inside WebView</em></strong></blockquote><h4>The Token Loop</h4><pre>[ User logs in via RN ]<br>          |<br>          v<br>[ RN gets auth token ]<br>          |<br>          v<br>[ RN loads WebView with short-lived token ]<br>          |<br>          v<br>[ Web establishes session (cookie-based) ]<br>          |<br>          v<br>[ Web needs refresh ]<br>          |<br>          v<br>[ dispatch(&quot;REQUEST_TOKEN&quot;) ]<br>          |<br>          v<br>[ RN returns fresh token ]</pre><p>RN:</p><ul><li>logs user in</li><li>owns token lifecycle</li></ul><p>Web:</p><ul><li>receives session context</li><li>requests tokens when needed</li></ul><h4>Token Refresh (two-layer defense)</h4><p>1. Proactive: <em>Web checks expiry:</em></p><pre>if (token.expiringSoon) → request refresh</pre><p>2. Reactive</p><pre>API fails → request token → retry once</pre><h4>Why this design holds</h4><ul><li>single source of truth (RN)</li><li>no dual session states</li><li>predictable refresh logic</li></ul><p>Web is not “logged in”.<br> It is <strong>operating under delegated identity</strong>.Subtle win</p><p>We didn’t just sync auth.</p><p>We <strong>centralized authority</strong>.</p><p>That distinction matters.</p><h3>🧭 Control Plane (this is what made it scalable)</h3><p>Hardcoding behavior across layers is where systems decay.</p><p>We avoided that by introducing a <strong>Control Plane</strong>.</p><p>RN sends config → Web adapts.</p><h4>Example</h4><pre>{<br>  &quot;allowedRoutes&quot;: [&quot;/ehr&quot;, &quot;/consult&quot;],<br>  &quot;capabilities&quot;: {<br>    &quot;videoCall&quot;: &quot;native&quot;,<br>    &quot;chat&quot;: &quot;native&quot;,<br>    &quot;documentPicker&quot;: &quot;native&quot;<br>  },<br>  &quot;ui&quot;: {<br>    &quot;header&quot;: &quot;native&quot;<br>  },<br>  &quot;appVersion&quot;: &quot;5.2.1&quot;<br>}</pre><h3>What this unlocked</h3><p>This wasn’t just config. It was <strong>runtime governance</strong>.</p><h4>1. Feature delegation</h4><p>We could switch native/web implementations without touching code and dealing with deployments via remote configs</p><pre>videoCall:<br>  native → use RN<br>  web    → use web SDK</pre><h4>2. Mini-app architecture</h4><p>Restrict routes → embed only part of the app → behave like a standalone module.</p><pre>Allowed routes:<br> /ehr<br> /consult<br>Anything else:<br> → exit WebView</pre><p>This turned parts of the app into <strong>controlled mini-modules</strong>.</p><h4>3. Platform reuse</h4><p>Same web app could run in:</p><ul><li>browser</li><li>mobile (RN)</li><li>iframe (partner integrations)</li></ul><p>Because behavior wasn’t hardcoded. It was <strong>configured</strong>.</p><h3>🧭 Navigation: one brain only</h3><p>Hybrid systems break when both sides think they’re in charge.</p><p>We enforced:</p><blockquote><strong><em>RN owns navigation. Always (almost)</em></strong></blockquote><h4>Flow</h4><pre>[ Web wants to go back ]<br>          |<br>          v<br>dispatch(&quot;GO_BACK&quot;)<br>          |<br>          v<br>[ RN decides navigation ]</pre><p>Web intent:</p><pre>dispatch(&quot;GO_BACK&quot;)</pre><p>RN decides:</p><pre>navigation.goBack()</pre><h4>Why this matters</h4><p>If both control navigation:</p><ul><li>history mismatch</li><li>broken back button</li><li>impossible bugs</li></ul><p>We removed that entire category by design.</p><h3>⚡ Performance: (honest, not idealistic)</h3><p>We weren’t naive. This comes with cost. But the benefits outweigh the cost and it worked for our use cases</p><h4>What we did</h4><ul><li>preloaded WebView before user reached it</li><li>cached static assets</li><li>avoided large payloads across bridge</li><li>reused RN data when possible</li></ul><h4>What still hurt</h4><ul><li>serialization cost across bridge</li><li>React web app re-render overhead</li><li>cold start (hidden, not eliminated)</li></ul><h4>Honest take</h4><blockquote><em>This system optimizes </em><strong><em>velocity and consistency</em></strong><em>, not raw performance.</em></blockquote><p>If performance is your top priority, this is not your pattern.</p><h3>📊 Analytics: one stream, not two</h3><pre>IF inside RN:<br>   Web → RN → analytics<br><br>ELSE:<br>   Web → analytics directly</pre><p>when outside RN use web, otherwise :</p><pre>dispatch(&quot;TRACK_EVENT&quot;, payload)</pre><h4>Why this matters</h4><p>Two analytics pipelines = inconsistent data.</p><p>We forced:</p><blockquote><strong><em>one session, one source</em></strong></blockquote><p>Even if it felt slightly over-engineered.</p><h3>🧨 Things that broke</h3><h4>1. Events fired before bridge ready</h4><pre>Web fires event<br>RN not ready<br>→ event lost</pre><p>Fix:</p><ul><li>handshake</li><li>retry mechanism</li></ul><h4>2. Token race</h4><pre>Multiple refresh calls<br>→ inconsistent token</pre><p>Fix:</p><ul><li>single-flight lock</li></ul><h4>3. Contract drift</h4><pre>Web sends unexpected payload<br>RN breaks silently</pre><p>Fix:</p><ul><li>SDK abstraction (partial)</li><li>still not perfect</li></ul><h3>⚖️ Tradeoffs</h3><ul><li>debugging spans two runtimes</li><li>performance ceiling exists</li><li>requires discipline or it collapses</li><li>abstraction can hide complexity until it bites</li></ul><h3>🧠 What this actually is</h3><p>Not WebView embedding.</p><p>Not micro-frontend.</p><p>Not just “hybrid app”.</p><blockquote><strong>A protocol-driven system coordinating two runtimes with strict ownership boundaries.</strong></blockquote><h3>🔮 If I rebuild this today</h3><ul><li>strongly typed event contracts (not semi-YOLO)</li><li>versioned protocol</li><li>smaller isolated web modules</li><li>explore direct native bridges (new RN architecture)</li></ul><h3>🧩 When to use this (and when not to)</h3><p>Use it when:</p><ul><li>your web app evolves faster than mobile</li><li>feature parity matters more than purity</li><li>workflows &gt; animations</li></ul><p>Avoid it when:</p><ul><li>performance is king</li><li>your team can’t enforce contracts</li></ul><h3>🧭 Closing</h3><p>This came out of real systems we built at MFine.</p><p>But the takeaway isn’t about that system.</p><p>It’s this:</p><blockquote><strong><em>If two platforms must coexist, don’t duplicate logic. Define a protocol and let them collaborate.</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0eb143aabee5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>