Skip to main content

State Sync Diagram

sync-state-bridge

A deterministic, race‑safe state synchronisation bridge for real‑time applications, with built‑in QoS, backpressure handling, and network resilience.

  • Versioned, per‑type sync – each entity type has its own version history.
  • Segmented deltas – send only what changed, with a manifest first.
  • FSM lifecycle – correct handling of add→update→delete sequences.
  • Resilient reconnection – out‑of‑order drop, monotonicity guards, schema mismatch handling.
  • Quality of Service (QoS) – three priority levels (CRITICAL, CONFLATABLE, BEST_EFFORT).
  • Adaptive backpressure – the Governor monitors queue depth and recommends throttling.
  • Pure Python + JS – no binary dependencies.
  • Pluggable transports – IPC, HTTP/SSE, and more.

Quick Start

Install the server package:

pip install sync-state-bridge

For optional features (faster JSON, serial transport):

pip install sync-state-bridge[fast,serial]

Basic Usage (Simple Server with StateSync)

from sync_state import StateSync, Presets

sync = StateSync()

# Register a snapshot provider with a QoS profile
sync.register_snapshot_provider(
    "vehicles",
    get_vehicles,
    qos=Presets.low_bandwidth()   # optimised for slow links
)

# After changes:
sync.mark_dirty("vehicles")
await sync.commit()

# Stream deltas over HTTP (SSE)
from fastapi import FastAPI, StreamingResponse
app = FastAPI()

@app.get("/stream")
async def stream(versions: str = "{}"):
    return StreamingResponse(
        sync.stream_deltas(json.loads(versions)),
        media_type="text/event-stream"
    )

Advanced Full‑Stack Example (Gateway + Worker)

For a production‑grade setup with separate gateway and worker processes, see the advanced_game example. It demonstrates:

  • Router – pure frame forwarder.
  • SynchronisationKernel – priority queue and backpressure.
  • Governor – adaptive health monitoring.
  • HTTPSSETransport – web interface.
  • IPCTransport – communication between gateway and worker.

Gateway (game_server.py)

from sync_state.core.sync_bridge import SyncStateBridge
from sync_state.core.ipc_transport import IPCTransport
from sync_state.web import HTTPSSETransport

bridge = SyncStateBridge()
ipc = IPCTransport(listen_on="127.0.0.1:8766")
http = HTTPSSETransport(host="127.0.0.1", port=8000, static_dir="static")

bridge.register_transport(ipc, type_list=["player"], direction="in")
bridge.register_transport(http, type_list=["player"], direction="out", qos_map={"player": 3})

bridge.start()
http.start()

Worker (game_worker.py)

from sync_state.core import StateSync
from sync_state.core.ipc_transport import IPCTransport

state_sync = StateSync()
# register snapshot providers...
ipc = IPCTransport(connect_to="127.0.0.1:8766")
# read frames, update state, commit, send deltas.

Socket Server (for low‑level IPC)

from sync_state.reliability import StateSyncSocketClient

client = StateSyncSocketClient(
    host="127.0.0.1",
    port=8765,
    on_delta_callback=handle_delta
)
await client.connect_and_listen()

Quality of Service (QoS)

Each entity type can have a QoS profile:

Policy Behaviour
CRITICAL Never dropped; queued until delivered.
CONFLATABLE Intermediate deltas dropped; only the latest is sent.
BEST_EFFORT Discarded immediately under queue pressure.

Pre‑configured profiles (Presets.conservative(), Presets.low_bandwidth(), Presets.high_throughput()) are provided.

Advanced Features

QoS‑Aware Priority Queue

The PriorityQoSQueue provides three tiers with automatic eviction.

Adaptive Governor

The Governor monitors queue depth and drop rates, providing recommendations to the application (e.g., max_particles, max_asteroids).

Snapshot Chunking

Large snapshots are split into 64 KB chunks for reliable transmission.

from sync_state.reliability import chunk_snapshot, SnapshotReassembler

chunks = chunk_snapshot(state, "snap_123")
reassembler = SnapshotReassembler()
for chunk in chunks:
    reassembler.ingest_chunk(chunk)

Testing

Run the unit tests:

pytest tests/

The test suite covers:

  • Deterministic hashing (canonical_hash)
  • Commit & delta generation
  • Version‑gap full‑snapshot recovery
  • Router, Kernel, and QoS logic

Demos

Run the demos:

  • Chat – real‑time message broadcast with shared history.
cd examples/chat && python server.py
cd examples/simple-game && python server.py
  • Advanced Game – spaceship game with adaptive QoS and monitoring (the most comprehensive demo).
cd examples/advanced-game && python game_start.py

These run on a local server, displayed in the web browser (at http:0.0.0.0:8000 by default). Proof of this is that you can refresh or close+reopen the browser window and it is still running.

Protocol

See PROTOCOL.md for the full SSE‑based delta protocol and the router/kernel design.

License

MIT

Metadata

Release files for sync-state-bridge 1.1.79

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sync-state-bridge 1.1.79
File Size Uploaded Provenance
sync_state_bridge-1.1.79.tar.gz 34.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for sync-state-bridge 1.1.79
File Interpreter ABI Platform
sync_state_bridge-1.1.79-py3-none-any.whl Python 3 none any Details

Total release size: 70.5 kB

Release files / sync_state_bridge-1.1.79.tar.gz

Download URL sync_state_bridge-1.1.79.tar.gz
Size 34.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a47a060d36b0da25a937e9bd24144fecb592ae5976792a9b71b237a458d5cac1
BLAKE2b-256 checksum
How to use checksums
4222f83c584809f8ec4b480e95808b4cc39da8ea78e5f9c2beb3a1c33e5a3804
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Release files / sync_state_bridge-1.1.79-py3-none-any.whl

Download URL sync_state_bridge-1.1.79-py3-none-any.whl
Size 36.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4a120f761c0d0ee1c79e6d9a562796df2891691ef07c44d087c4b588192a016c
BLAKE2b-256 checksum
How to use checksums
4f969d56204fdea502b2e12f2874255c961cb81a333a7fcf789aa28a25528022
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.13

Security for sync-state-bridge 1.1.79

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page