Skip to main content

Versioned delta-state synchronization engine for real-time systems, IPC, and embedded applications

Project description

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) – define drop policies (CRITICAL, CONFLATABLE, BEST_EFFORT) per entity type.
  • Backpressure‑aware transports – bounded queues, priority full‑snapshot injection, congestion metrics.
  • Reconnecting client – exponential backoff, version tracking, automatic recovery.
  • Pure Python + JS – no binary dependencies.

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

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"
    )

Socket Server (for low‑level IPC)

from sync_state.transports import StateSyncSocketServer

server = StateSyncSocketServer(sync)
await server.start_tcp(host="0.0.0.0", port=8765)

Reconnecting Client

from sync_state import StateSyncSocketClient

def handle_delta(delta):
    print(f"Update: {delta}")

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 (ideal for high‑frequency telemetry).
BEST_EFFORT Discarded immediately under queue pressure.

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

Testing

Run the unit tests:

pytest tests/

The test suite covers:

  • Deterministic hashing (canonical_hash)
  • Commit & delta generation
  • Version‑gap full‑snapshot recovery

Demos

  • Chat – real‑time message broadcast with shared history.
  • Game – "Find the Clusters" demonstrating turn‑based sync.

Run the demos:

cd examples/chat && python server.py
cd examples/game && python server.py

Protocol

See PROTOCOL.md for the full SSE‑based delta protocol.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sync_state_bridge-1.0.3.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sync_state_bridge-1.0.3-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file sync_state_bridge-1.0.3.tar.gz.

File metadata

  • Download URL: sync_state_bridge-1.0.3.tar.gz
  • Upload date:
  • Size: 14.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.13

File hashes

Hashes for sync_state_bridge-1.0.3.tar.gz
Algorithm Hash digest
SHA256 32e19198bd10172d4b6590cdad2e655b4795aa7da8d31bc1579c0928c0ac43cd
MD5 9511a9be50f2a1df3704c12bcf1ad21d
BLAKE2b-256 34aaa08aaf4e5760ad21d7b94c536c462d6890aaba45310c44e4df8948db703e

See more details on using hashes here.

File details

Details for the file sync_state_bridge-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sync_state_bridge-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e5cb5e98f4a31c3c27093251fc4188c976372048e04d6f703bcb8f7e3caadbf2
MD5 2075355d3d08051362171e4f6ac7befd
BLAKE2b-256 879e937696f6f691b560b30b21c7e2d7a810505525fa844771f1ae73a20c30d3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page