<?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: Praveen</title>
    <description>The latest articles on DEV Community by Praveen (@shenoy18).</description>
    <link>https://dev.to/shenoy18</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%2F4261802%2Ffe4c0eca-853c-4841-bdc7-50fae5dda32b.png</url>
      <title>DEV Community: Praveen</title>
      <link>https://dev.to/shenoy18</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shenoy18"/>
    <language>en</language>
    <item>
      <title>How online 2-player works in a browser fighting game: lockstep netcode on a $5 server</title>
      <dc:creator>Praveen</dc:creator>
      <pubDate>Tue, 24 Nov 2026 05:07:34 +0000</pubDate>
      <link>https://dev.to/shenoy18/how-online-2-player-works-in-a-browser-fighting-game-lockstep-netcode-on-a-5-server-4f5a</link>
      <guid>https://dev.to/shenoy18/how-online-2-player-works-in-a-browser-fighting-game-lockstep-netcode-on-a-5-server-4f5a</guid>
      <description>&lt;p&gt;&lt;a href="https://hostiletakedown.com/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=netcode" rel="noopener noreferrer"&gt;Hostile Takedown&lt;/a&gt; is a parody fighting game that runs in the browser, and it has a "Play a friend" mode: one player creates a room, shares a 4-digit code, and the two of you fight online. Here's how that works without a big game server.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lockstep instead of a server simulation
&lt;/h2&gt;

&lt;p&gt;The server never runs the fight. Both browsers run the exact same simulation at a fixed 60 steps per second, and the only thing that crosses the network is each player's controller input for each frame, packed into a small bitmask (left, right, duck, block, punch, kick, grab, signature, two supers, jump).&lt;/p&gt;

&lt;p&gt;For that to work, both sides must compute identical results from identical inputs. The tricky part is randomness: crits, AI flourishes and particle choices all used &lt;code&gt;Math.random()&lt;/code&gt;. During an online match the game swaps it for a small seeded generator (mulberry32), with the seed dealt by the server when the match starts. Purely cosmetic systems (audio, the 3D crowd) keep their own generator so they can't consume numbers from the shared sequence and push the two sides apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hiding latency with input delay
&lt;/h2&gt;

&lt;p&gt;Each input is scheduled a few frames into the future, so it normally reaches the other player before that frame is simulated. The delay isn't fixed: while you wait in the lobby, the host pings the friend through the relay, takes the slower round trips (80th percentile), and sets the delay to cover half of that plus a safety margin, between 4 and 16 frames. Players on the same city network get a snappy game; players across continents get a slightly heavier one that doesn't stall.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping both sides honest
&lt;/h2&gt;

&lt;p&gt;Every 30 frames the host sends a compact snapshot (positions, health, meter, round wins). If the guest has drifted, it nudges its state toward the host's using its own history for that frame. Rendering is interpolated between simulation steps, so motion stays smooth on 120 Hz phones and when a frame occasionally has to wait for a late input.&lt;/p&gt;

&lt;h2&gt;
  
  
  Phones and dropped connections
&lt;/h2&gt;

&lt;p&gt;Phones kill WebSockets the moment you switch apps to send the room code. Each seat gets a secret token stored in the browser, and the server holds a dropped seat for 90 seconds, so you can share the link on WhatsApp, come back and still be the host. If a connection drops in the middle of a fight, both players return to the pick screen with the same CEOs instead of the match silently desyncing.&lt;/p&gt;

&lt;h2&gt;
  
  
  The server
&lt;/h2&gt;

&lt;p&gt;The relay is about 200 lines of dependency-free Node: a minimal WebSocket implementation on Node's own &lt;code&gt;http&lt;/code&gt; module, rooms with two seats, a heartbeat, and message relaying. It runs on a $5/month Lightsail instance behind Caddy for HTTPS.&lt;/p&gt;

&lt;p&gt;If you try the online mode with a friend, I'd love to hear how it felt on your connection: &lt;a href="https://hostiletakedown.com/?utm_source=devto&amp;amp;utm_medium=article&amp;amp;utm_campaign=netcode" rel="noopener noreferrer"&gt;play Hostile Takedown&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>gamedev</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>networking</category>
    </item>
  </channel>
</rss>
