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
- Chat – real‑time message broadcast with shared history.
- Battleships – turn‑based game using StateSync.
- Advanced Game – asteroids game with adaptive QoS and monitoring (the most comprehensive demo).
Run the demos:
cd examples/chat && python server.py
cd examples/battleships && python server.py
cd examples/advanced_game && python game_start.py
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.78
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| sync_state_bridge-1.1.78.tar.gz | 34.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sync_state_bridge-1.1.78-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 70.2 kB
Release files / sync_state_bridge-1.1.78.tar.gz
| Download URL | sync_state_bridge-1.1.78.tar.gz |
|---|---|
| Size | 34.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c73bcd6b959659ad9a3818fadadbb06a690a6ab46556198e9820696c862b47ee
|
|
BLAKE2b-256 checksum How to use checksums |
4caa760e8fb8af430c1df405f57c5943eaadac81b0aeb0ce2c03acd0b94f2e88
|
| 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.78-py3-none-any.whl
| Download URL | sync_state_bridge-1.1.78-py3-none-any.whl |
|---|---|
| Size | 36.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e4e2abb31980edfea5d96d635833a76b8b7438cec7fa0f91fca82143c74cc0cb
|
|
BLAKE2b-256 checksum How to use checksums |
af79e272149cdbf9d488648c0df7e65503810f0821c9ac341d6d1c92512e0a60
|
| 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.78
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.