WhoBuilt

Building Real-Time Multi-User Apps With JSON Patches

Oct 22, 2020· 26 min· 546 views·Watch on YouTube

Speaker

Sanketh Sahu, software engineer and CEO at GeekyAnts, explains how JSON patches can support real-time multi-user apps in a React Native EU presentation, drawing on the design tool BuilderX.

JSON patches as state updates

A JSON patch describes a change to an object with an operation, path, and value. Sahu shows adding properties to an empty object, then applying a patch through a Redux reducer using Immer’s `applyPatches`. Patches can update application state without writing a separate reducer for each change.

Storing and sharing changes

Storing patches preserves the sequence of changes, which can support version history and reconstructing earlier states. It can take more storage and computation to rebuild a state, and a snapshot alone cannot reveal the patches that produced it. Because patches are serializable, they can also sync document changes between threads or over a network.

Sync State and targeted updates

Sahu introduces Sync State, an experimental React state library built on Redux and Immer. Its `useDoc` hook and `setDoc` callback let components read and update a shared document while Immer generates patches behind the scenes. Sync State uses path-based listeners so updates reach components using the changed data, instead of running selectors for every connected component.

History and multiplayer conflicts

The history plugin stores patches for undo and redo rather than full state snapshots. For multiplayer, the remote plugin buffers local patches until the server accepts them, reverses rejected changes, and handles ordering. Sahu’s examples show unrelated edits, such as changing color and size, both succeeding; when clients change the same property, the server’s last accepted value becomes the source of truth. Undo and redo stacks remain local to each client.

Early release and planned extensions

Sahu describes Sync State as an early, evolving release and invites community feedback. The team was exploring REST and GraphQL plugins to sync local document mutations with backend data, along with plugins for forms, API calls, and persistent storage. He credits Himanshu, Rohit, Suraj, and Ankur for work on the release and documentation.

Takeaways

  • Use patches when you need a portable record of document changes that can be replayed across clients or threads.
  • Patch history can provide undo, redo, and versioning without storing a full snapshot for every change.
  • For responsive interfaces, subscribe components to the document paths they use so unrelated updates do not trigger them.
  • Define server rules for patch ordering, rejection, and conflicting edits before building collaborative features.
  • Keep undo and redo history client-local when one user’s undo should not reverse another user’s work.

Mentioned in this talk

Search inside this talk

What was claimed 12

  • feature
    BuilderX: Real-time multiplayer access to shared design documents
    “Recently, we added a sort of real-time multiplayer feature”
    YouTube
    Unverified1 source· Talk
  • role at
    Sanketh Sahu is software engineer at GeekyAnts
    “I'm Sanketh, a software engineer and CEO at GeekyAnts.”
    YouTube
    Unverified1 source· Talk
  • ceo of
    CEO
    “I'm Sanketh, a software engineer and CEO at GeekyAnts.”
    YouTube
    Unverified1 source· Talk
  • feature
    Sync State: History and remote syncing for React documents
    “works on the document and makes it easy for anyone trying to build real-time, multi-user apps.”
    YouTube
    Unverified1 source· Talk
  • built
    “we’ve tried to pull them into a separate npm library called Sync State.”
    YouTube
    Unverified1 source· Talk
  • uses
    “It’s all based on JSON patches for actions.”
    YouTube
    Unverified1 source· Talk
  • uses
    “It uses Redux internally and Immer to make things easy on the surface.”
    YouTube
    Unverified1 source· Talk
  • uses
    “Sync State is based on Redux. It uses Redux internally”
    YouTube
    Unverified1 source· Talk
  • feature
    Immer: Generates JSON Patches from draft state changes
    “you make changes to the draft state, and it generates a bunch of patches”
    YouTube
    Unverified1 source· Talk
  • feature
    Sync State: Push-based path updates that notify components using changed data
    “we pass down the path. The component fetches the data at that path in the document, then adds a listener to that path.”
    YouTube
    Unverified1 source· Talk
  • feature
    Sync State: History plugin provides undo and redo using patches
    “we have a history plugin, which helps you add undo and redo functionality to your document.”
    YouTube
    Unverified1 source· Talk
  • feature
    Sync State: Remote plugin handles patch buffering, reversal, and ordering
    “we need to buffer patches until the server accepts them. If the server rejects a patch as invalid, we need a way to reverse it.”
    YouTube
    Unverified1 source· Talk

Related talks