<?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: Harsh Pandey</title>
    <description>The latest articles on DEV Community by Harsh Pandey (@hpx7).</description>
    <link>https://dev.to/hpx7</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F844712%2F699804ce-9f08-434d-b1da-19d7a5fdc8d6.png</url>
      <title>DEV Community: Harsh Pandey</title>
      <link>https://dev.to/hpx7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hpx7"/>
    <language>en</language>
    <item>
      <title>Scalable WebSocket Architecture</title>
      <dc:creator>Harsh Pandey</dc:creator>
      <pubDate>Wed, 28 Sep 2022 16:12:48 +0000</pubDate>
      <link>https://dev.to/hpx7/scalable-websocket-architecture-4e1n</link>
      <guid>https://dev.to/hpx7/scalable-websocket-architecture-4e1n</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;The web has rapidly evolved from its origins of websites serving static HTML content, to today's highly dynamic web applications. Whether it's games, chat, or productivity tools, users expect the apps to be up-to-date and enable real-time interactions with others.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API"&gt;WebSockets&lt;/a&gt;, which enables real-time communication from within web browsers, are one of the central pieces that has enabled this level of interactivity. And yet, despite the wide spread adoption of WebSockets, surprisingly little has been written about effective ways to implement them at scale. Most current efforts try to retrofit &lt;a href="https://12factor.net/"&gt;techniques&lt;/a&gt; proven to scale traditional request/response applications (stateless) to real-time/streaming use cases (stateful), leading to architectural complexity and limitations.&lt;/p&gt;

&lt;p&gt;In this post, we will explore potential architectures for a typical realtime application: a turn-based multiplayer game. We'll start first with the commonly recommended architecture from the massively popular WebSocket library &lt;a href="https://socket.io/"&gt;Socket.IO&lt;/a&gt;, and then we'll propose an alternative architecture and detail some of its benefits.&lt;/p&gt;

&lt;h1&gt;
  
  
  Architecture Options
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;We are designing a backend system for a real-time turn-based game similar to online Chess or Monopoly. Users are grouped into sessions or "rooms" together, where users within a room interact with each other and there can be many concurrent rooms at any given time (in the chess example, a live chess match corresponds to an active room). To support a large number of concurrent rooms, we'd like a horizontally scalable architecture where adding additional game servers increases the number of concurrent rooms/players that our system can support.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traditional Socket.IO architecture
&lt;/h2&gt;

&lt;p&gt;The most commonly cited architecture for scalable WebSocket backends can be found on the Socket.IO documentation. Players are routed via a generic load balancer to one of many available server instances, and the server instances communicate with each other via a message broker. State is maintained in a globally shared database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v_OOIBG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brtzy23375x8zlcrk0pj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v_OOIBG7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/brtzy23375x8zlcrk0pj.png" alt="Image description" width="880" height="968"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Flow&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user from US East sends a message (e.g. a chess move) over a WebSocket connection scoped to a particular room/session (e.g. wss://example.com/room1). The connection is established with a load balancer like nginx, HAProxy, Amazon CloudFront, etc.&lt;/li&gt;
&lt;li&gt;The load balancer forwards the messages to a custom WebSocket server. When initially establishing the connection, the load balancer has to choose which WebSocket server instance to forward the connection to. To reduce latency, it should ideally pick one in the closest region to the user (using something like GeoDNS).&lt;/li&gt;
&lt;li&gt;The WebSocket server processes the message and issues a write query to a globally available database system like Postgres in order to update the game state. The database operation must be atomic to account for the fact that writes may also be coming from other WebSocket servers.&lt;/li&gt;
&lt;li&gt;The WebSocket server sends the game state update (e.g. the current chessboard position) to a message broker like Redis or RabbitMQ so that users connected to other WebSocket servers can receive the update.&lt;/li&gt;
&lt;li&gt;The message broker broadcasts the state update to all connected WebSocket servers.&lt;/li&gt;
&lt;li&gt;The WebSocket servers forward the state update (via the load balancer) to all WebSocket connections for the relevant room.&lt;/li&gt;
&lt;li&gt;The load balancer forwards the update over the client WebSocket connection.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Architecture using a Stateful Router
&lt;/h2&gt;

&lt;p&gt;This is an alternative architecture which relies on a specialized stateful proxy to group users to server instances based on their room. This "colocation" of users localizes data to the server instance and eliminates the need for a separate message broker.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bX900I73--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j863qfe81slaqh0oaigx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bX900I73--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j863qfe81slaqh0oaigx.png" alt="Image description" width="880" height="995"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Flow&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A user from US East sends a message over a WebSocket connection scoped to a particular room/session (e.g. wss://example.com/room1). The connection is established with the Stateful Router.&lt;/li&gt;
&lt;li&gt;The proxy forwards the messages to a custom WebSocket server. When initially establishing the connection, the proxy selects a WebSocket server for the requested room based on proximity to the user. For all subsequent connections to this roomId, the Stateful Router uses its internal map to route the connection to the same WebSocket server.&lt;/li&gt;
&lt;li&gt;The WebSocket server processes the message and issues a write query to a database in order to update the game state. Note that because all messages in the room are processed by a singular server, this database can be local to this server – this can be as simple as updating some in-memory data structure inside the server process, or a locally embedded database like SQLite/RocksDB.&lt;/li&gt;
&lt;li&gt;The WebSocket server sends the game state update to the proxy.&lt;/li&gt;
&lt;li&gt;The Stateful Router broadcasts the state update to either one or optionally all connected WebSocket clients in the room.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Advantages of Stateful Router
&lt;/h2&gt;

&lt;p&gt;Migrating from a message-broker based architecture to one utilizing a Stateful Router offers a number of advantages:&lt;/p&gt;

&lt;p&gt;1) Fewer messages (i.e. improved scalability)&lt;/p&gt;

&lt;p&gt;Before: Each WebSocket server needs to process every state update, filter messages addressed to connected clients and forward it along. Thus the scalability of the system is limited by the maximum throughput of a single server for processing state updates.&lt;/p&gt;

&lt;p&gt;After: Only one server needs to process the messages and state updates for a given room. As long as the largest/busiest room can be handled on a single server, this architecture is infinitely scalable.&lt;/p&gt;

&lt;p&gt;2) Reduced server load (via connection multiplexing)&lt;/p&gt;

&lt;p&gt;Before: Load balancers and servers maintain dedicated WebSocket connections for each client connected to them. This consumes resources (CPU, memory, file descriptors) on the servers.&lt;/p&gt;

&lt;p&gt;After: Servers only have a single connection to the Stateful Router. The proxy combines all incoming and outgoing messages over this single connection – this technique is known as connection multiplexing and it serves to shield the servers from concurrent user load. No more worries about file description limit and memory constraints.&lt;/p&gt;

&lt;p&gt;3) Data locality (via client colocation)&lt;/p&gt;

&lt;p&gt;Before: Servers write to a shared database and need to ensure a data consistency model (e.g. via atomic write operations or CRDTs).&lt;/p&gt;

&lt;p&gt;After: The data for a room is local to the server, allowing use of local in-memory or embedded databases – consistency becomes trivial. Additionally, if a persistence layer is used, it would only need consistency guarantees within a geography, without having to worry about global replication lag.&lt;/p&gt;

&lt;h1&gt;
  
  
  Hathora
&lt;/h1&gt;

&lt;p&gt;At &lt;a href="https://hathora.dev/"&gt;Hathora&lt;/a&gt;, our mission is to make it easier for developers to build, launch, and scale multiplayer games. One of the core technologies we have built is the Hathora Coordinator, which is our fully managed multi-tenant implementation of a Stateful Router.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hathora Coordinator
&lt;/h2&gt;

&lt;p&gt;An overview of the Hathora Coordinator and how it's used to power multiplayer games can be found in the &lt;a href="https://docs.hathora.dev/#/architecture"&gt;Hathora documentation&lt;/a&gt;. In addition to the stateful routing functionality described in this article, the Hathora Coordinator also implements a few other key features:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Edge networking&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To minimize latency, it's important to accept and process traffic closest to where users are (we wrote a &lt;a href="https://blog.hathora.dev/modern-cloud-for-multiplayer-games/"&gt;blog post&lt;/a&gt; going into more detail about this). The Hathora Coordinator is deployed across 8+ regions globally and accepts traffic at the edge before routing it to the right server instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transports&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We talked about WebSockets in this article, but the same techniques apply to other persistent transport types as well. The Hathora Coordinator also supports direct TCP connections, with UDP and WebTransport support coming soon. These connections are handled entirely by the Coordinator in a way such that your servers don't know or care about which transport type the clients originally connected with, greatly simplifying your servers networking code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Analytics&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since all traffic goes through the coordinator, it has access to important information like the number of concurrent connections over time, which geographies the connections originate from, the average connection duration, etc. Having access to this information can be invaluable for understanding your user base and engagement.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Hathora: Multiplayer Made Easy</title>
      <dc:creator>Harsh Pandey</dc:creator>
      <pubDate>Tue, 19 Apr 2022 16:21:16 +0000</pubDate>
      <link>https://dev.to/hathora/hathora-multiplayer-made-easy-36cl</link>
      <guid>https://dev.to/hathora/hathora-multiplayer-made-easy-36cl</guid>
      <description>&lt;p&gt;More multiplayer games are being developed than ever before, but building &amp;amp; launching a successful online multiplayer game remains one of the most notoriously difficult endeavors in the software world. The biggest challenges developers face are (1) choosing and correctly implementing the technologies to enable multiplayer, and (2) ending up with an architecture that you can operate and scale.&lt;/p&gt;

&lt;p&gt;If you are starting a new game from scratch, you are inundated by a multitude of choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;How should I set up my project structure?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Should I use peer-to-peer or client-server architecture?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;TCP, UDP, WebSockets, or WebRTC for networking?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;How should I implement authentication?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;How do I persist data?&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For the amount of demand for multiplayer games, the development process should not be as hard as it is today. Armed with a passion for multiplayer games and extensive experience building &amp;amp; operating scalable systems, we have been working on a solution. Enter &lt;a href="https://hathora.dev/"&gt;Hathora&lt;/a&gt;: a framework for building multiplayer games.&lt;/p&gt;

&lt;p&gt;Hathora can be used for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;🎲 Turn-based multiplayer games - Examples: Chess, Words with Friends, Codenames, etc&lt;/li&gt;
&lt;li&gt;🎮 Realtime multiplayer games - Examples: Among Us, Agar.io, Slither.io, etc&lt;/li&gt;
&lt;li&gt;💬 Realtime &amp;amp; social applications - Examples: chat apps, delivery tracking apps, etc&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We've built Hathora to provide the most streamlined development experience when starting a multiplayer game from scratch. The framework provides the right abstractions such that a game can be prototyped in minutes and scaled to millions of users.&lt;/p&gt;




&lt;h1&gt;
  
  
  Prototype in Minutes
&lt;/h1&gt;

&lt;p&gt;Hathora's philosophy is to minimize the burden on the developer by allowing them to focus only on functionality specific to their game, while the framework serves as the engine to power the remaining machinery. We are pushing the limits of game development to drastically reduce the time it takes to ship a game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Autogenerate type-safe data model and clients
&lt;/h2&gt;

&lt;p&gt;All Hathora projects start by defining their API using Hathora's language agnostic declarative &lt;a href="https://docs.hathora.dev/#/hathora-yml"&gt;API format&lt;/a&gt;. From this definition, Hathora generates typesafe data models and clients for the language of your choice.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hathora frees you from the extensive boilerplate normally required for client/server applications.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MralBkjN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxks7t20pyatl7t25t7z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MralBkjN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxks7t20pyatl7t25t7z.png" alt="Hathora codegen" width="880" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Write server logic in 20-100x fewer lines of code
&lt;/h2&gt;

&lt;p&gt;Write backend logic seamlessly in the language of your choice. Just read and update in memory state, no database queries or ORMs needed – &lt;em&gt;Hathora automatically manages &lt;a href="https://docs.hathora.dev/#/state?id=persistence"&gt;persistence&lt;/a&gt; behind the scenes&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r298gGhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttqtgwf9r50tojk6tkfc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r298gGhu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttqtgwf9r50tojk6tkfc.png" alt="Hathora backend logic" width="880" height="705"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterate faster with the Prototype UI
&lt;/h2&gt;

&lt;p&gt;Test your backend logic without writing any frontend code using the built in &lt;a href="https://docs.hathora.dev/#/type-driven-development?id=prototype-ui"&gt;Prototype UI&lt;/a&gt;. Create &amp;amp; join rooms, call server methods, and interact with live data all from within your browser.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hathora enables play testing from the first minute.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cYw-fgJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8c75pcxfulnackd4tn6v.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cYw-fgJl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8c75pcxfulnackd4tn6v.gif" alt="Hathora Prototype UI" width="880" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Flexible, cross-platform client support
&lt;/h2&gt;

&lt;p&gt;Once the backend functionality has been verified, it’s easy to use the generated client to &lt;a href="https://docs.hathora.dev/#/client?id=fully-custom-frontend"&gt;build your UI&lt;/a&gt; in your platform and technologies of choice and share the game with the world.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The Hathora client fully handles networking, and easily integrates into any frontend technology/platform of your choice.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mwO3QxcX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0vozwzyfmd6zbia52wt.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mwO3QxcX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v0vozwzyfmd6zbia52wt.gif" alt="Hathora custom UI" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other features you get out of the box with Hathora:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Automatic &lt;a href="https://docs.hathora.dev/#/data-flow"&gt;state synchronization&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Optimized &lt;a href="https://docs.hathora.dev/#/networking"&gt;networking&lt;/a&gt; (binary protocol, delta encoding)&lt;/li&gt;
&lt;li&gt;Built in &lt;a href="https://docs.hathora.dev/#/auth"&gt;authentication&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Project structure generation &amp;amp; development server with hot reloading&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  Scale to Millions
&lt;/h1&gt;

&lt;p&gt;Once you've built &amp;amp; released your game, you don't want to spend the next 6 months battling scaling issues and reworking the architecture to meet your user demand. Hathora includes several key components that make it so you can be production-ready from day 1.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hathora Coordinator
&lt;/h2&gt;

&lt;p&gt;Central to the Hathora architecture is the managed Coordinator service, which sits between Hathora clients and Hathora backends.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DtLfAkgw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aceffybqevznfbvjzq4z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DtLfAkgw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aceffybqevznfbvjzq4z.png" alt="Hathora coordinator" width="880" height="1046"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The coordinator plays several key roles in the architecture:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It ensures consistent routing so that clients playing the same game get routed to the same backend instance.&lt;/li&gt;
&lt;li&gt;It load balances games between backend instances to ensure an even distribution of traffic.&lt;/li&gt;
&lt;li&gt;It reduces latency by dynamically assigning games to instances based on the geographic distribution of the connecting users.&lt;/li&gt;
&lt;li&gt;It handles backend failover by automatically migrating the games from the failed instance to a new instance, all of which is transparent to clients.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Hathora Protocol
&lt;/h2&gt;

&lt;p&gt;The Hathora Protocol governs the communication between clients and servers. It is flexible, lightweight, platform &amp;amp; transport agnostic.&lt;/p&gt;

&lt;p&gt;At the core of the protocol lies a highly optimized serialization format. All data is binary encoded and servers send only delta updates to clients after the initial snapshot. The encoding/decoding code is generated from the API definition and is incredibly efficient so as to minimize latency while sending and receiving messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hathora Cloud
&lt;/h2&gt;

&lt;p&gt;Although it will always remain possible to self host your backend instances, for those who do not wish to manage their own infrastructure we have built Hathora Cloud.&lt;/p&gt;

&lt;p&gt;Hathora Cloud is a platform for running Hathora applications at scale. It has features like autoscaling, multi-region deployments, monitoring &amp;amp; analytics, and migration management. Hathora Cloud makes it simple to deploy your game servers globally and scale to millions of users.&lt;/p&gt;

&lt;p&gt;Hathora Cloud is currently in private alpha, sign up using &lt;a href="https://docs.google.com/forms/d/e/1FAIpQLSeGhyNzKozcxNj4zgxi0w3mxup58752JBqCum63-HuHX7X30Q/viewform"&gt;this form&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://docs.hathora.dev/#/"&gt;Get started with Hathora today.&lt;/a&gt;&lt;/p&gt;

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