| Crates.io | tx2-link |
| lib.rs | tx2-link |
| version | 0.1.1 |
| created_at | 2025-11-27 17:03:27.977875+00 |
| updated_at | 2025-11-28 22:38:35.859732+00 |
| description | Binary protocol for syncing ECS worlds with field-level delta compression |
| homepage | https://github.com/IreGaddr/tx2-link |
| repository | https://github.com/IreGaddr/tx2-link |
| max_upload_size | |
| id | 1954110 |
| size | 152,162 |
Binary protocol for syncing ECS worlds between runtimes with field-level delta compression.
tx2-link is the bridge/protocol layer of the TX-2 ecosystem, enabling efficient synchronization of Entity-Component-System state across web, native, and CLI environments. It defines the "wire format" for transmitting world snapshots and deltas with minimal bandwidth overhead.
TX2_DEBUG=1 or TX2_TRACE=1use tx2_link::{WorldSnapshot, DeltaCompressor};
let mut compressor = DeltaCompressor::new();
// Create two snapshots
let snapshot1 = WorldSnapshot { /* ... */ };
let snapshot2 = WorldSnapshot { /* ... */ };
// Generate delta (only changed fields)
let delta = compressor.create_delta(&snapshot1, &snapshot2)?;
// Apply delta to reconstruct snapshot2
let reconstructed = compressor.apply_delta(&snapshot1, &delta)?;
assert_eq!(snapshot2, reconstructed);
use tx2_link::{Message, Serializer, SerializationFormat};
// Create message
let message = Message::Snapshot(snapshot);
// Serialize to MessagePack
let mut serializer = Serializer::new(SerializationFormat::MessagePack);
let bytes = serializer.serialize(&message)?;
// Deserialize
let deserialized: Message = serializer.deserialize(&bytes)?;
use tx2_link::{Transport, WebSocketTransport};
// Create WebSocket transport
let transport = WebSocketTransport::connect("ws://localhost:8080").await?;
// Send message
transport.send(&message).await?;
// Receive message
let received = transport.receive().await?;
use tx2_link::{RateLimiter, TokenBucketLimiter};
// Create rate limiter: 100 msg/sec, burst of 10
let mut limiter = TokenBucketLimiter::new(100.0, 10);
// Check if message can be sent
if limiter.check_message(1)? {
transport.send(&message).await?;
}
Benchmarked on 10,000 entities with Position, Velocity, Health components:
| Format | Serialize | Deserialize | Size |
|---|---|---|---|
| MessagePack | 180µs | 250µs | 1.05MB |
| Bincode | 140µs | 195µs | 1.12MB |
| JSON | 420µs | 350µs | 2.28MB |
pub enum Message {
Snapshot(WorldSnapshot), // Full world state
Delta(DeltaSnapshot), // Incremental update
EntityCreated { id, components }, // New entity
EntityDeleted { id }, // Entity removed
ComponentAdded { entity, data }, // Component attached
ComponentRemoved { entity, id }, // Component detached
SchemaUpdate(ComponentSchema), // Type definition
}
pub struct WorldSnapshot {
pub timestamp: u64,
pub entities: Vec<EntitySnapshot>,
}
pub struct EntitySnapshot {
pub id: EntityId,
pub components: Vec<ComponentSnapshot>,
}
pub struct ComponentSnapshot {
pub id: ComponentId,
pub data: ComponentData,
}
pub enum ComponentData {
Binary(Vec<u8>), // Raw bytes
Json(String), // JSON string
Structured(HashMap<FieldId, FieldValue>), // Field-level access
}
tx2-link uses field-level diffing for maximum compression:
For structured components:
// Previous: { x: 10.0, y: 20.0, z: 30.0 }
// Current: { x: 10.0, y: 25.0, z: 30.0 }
// Delta: { y: 25.0 } // Only y changed
All transports implement the Transport trait:
#[async_trait]
pub trait Transport: Send + Sync {
async fn send(&self, message: &Message) -> Result<()>;
async fn receive(&self) -> Result<Message>;
async fn close(&self) -> Result<()>;
}
use tx2_link::WebSocketTransport;
// Server
let transport = WebSocketTransport::bind("127.0.0.1:8080").await?;
// Client
let transport = WebSocketTransport::connect("ws://localhost:8080").await?;
use tx2_link::IpcTransport;
// Create named pipe/socket
let transport = IpcTransport::new("tx2-world")?;
use tx2_link::StdioTransport;
// Use stdin/stdout
let transport = StdioTransport::new();
use tx2_link::MemoryTransport;
// In-process channels (for testing)
let (tx, rx) = MemoryTransport::create_pair();
Allows bursts up to a capacity, refilling at a steady rate:
use tx2_link::TokenBucketLimiter;
// 1000 msg/sec, burst of 100
let limiter = TokenBucketLimiter::new(1000.0, 100);
// Check and consume tokens
if limiter.check_message(1)? {
// Send message
}
Enforces strict limits over a time window:
use tx2_link::SlidingWindowLimiter;
// 1000 msg/sec, 1MB/sec, 60-second window
let limiter = SlidingWindowLimiter::new(
1000, // max messages
1_000_000, // max bytes
60.0, // window seconds
);
if limiter.check(1, message_size)? {
// Send message
}
use tx2_link::{SchemaRegistry, ComponentSchema};
let mut registry = SchemaRegistry::new();
// Register component type
let schema = ComponentSchema::new("Position")
.with_field("x", FieldType::F32)
.with_field("y", FieldType::F32)
.with_field("z", FieldType::F32)
.with_version(1);
registry.register(schema)?;
// Validate incoming data
if registry.validate("Position", &component_data)? {
// Apply update
}
tx2-link bridges the TX-2 stack:
use tx2_link::*;
// Server
let transport = WebSocketTransport::bind("127.0.0.1:8080").await?;
let limiter = TokenBucketLimiter::new(100.0, 10);
let mut compressor = DeltaCompressor::new();
let mut last_snapshot = world.create_snapshot();
loop {
tokio::time::sleep(Duration::from_millis(16)).await; // 60 FPS
let snapshot = world.create_snapshot();
let delta = compressor.create_delta(&last_snapshot, &snapshot)?;
if limiter.check_message(1)? {
transport.send(&Message::Delta(delta)).await?;
}
last_snapshot = snapshot;
}
// Client
let transport = WebSocketTransport::connect("ws://localhost:8080").await?;
let mut compressor = DeltaCompressor::new();
let mut snapshot = WorldSnapshot::empty();
loop {
let message = transport.receive().await?;
match message {
Message::Snapshot(full) => {
snapshot = full;
world.restore_from_snapshot(&snapshot)?;
}
Message::Delta(delta) => {
snapshot = compressor.apply_delta(&snapshot, &delta)?;
world.restore_from_snapshot(&snapshot)?;
}
_ => {}
}
}
cargo test
All 22 tests should pass, covering:
cargo bench
Benchmarks measure:
tx2-link includes a comprehensive debug system for inspecting protocol operations without modifying code.
Enable debug features using environment variables:
TX2_DEBUG=1 or TX2_DEBUG_JSON=1 - Enable JSON pretty-printing of all messages, snapshots, and deltasTX2_TRACE=1 - Enable human-readable trace logging with operation timings and sizesBoth can be combined: TX2_DEBUG=1 TX2_TRACE=1
When TX2_DEBUG=1 is set:
When TX2_TRACE=1 is set:
# Run with JSON debug logging
TX2_DEBUG=1 cargo run
# Run with human-readable traces
TX2_TRACE=1 cargo run
# Run with both
TX2_DEBUG=1 TX2_TRACE=1 cargo run
With TX2_TRACE=1:
[TX2-LINK] Delta Summary:
Timestamp: 2.0 (base: 1.0)
Total changes: 5
+ 1 entities added
~ 2 components modified
[TX2-LINK] Delta compression: 1232875 bytes → 1052 bytes (1171.79× reduction) in 2134µs
[TX2-LINK] Serialized 1052 bytes using MessagePack in 250µs
With TX2_DEBUG=1:
[TX2-LINK] Serialized Message:
{
"header": {
"msg_type": "Delta",
"sequence": 42,
"timestamp": 1234567890
},
"payload": {
"changes": [
{
"EntityAdded": { "entity_id": 123 }
},
{
"ComponentAdded": {
"entity_id": 123,
"component_id": "Position",
"data": { "x": 10.0, "y": 20.0 }
}
}
]
}
}
You can also enable debug mode programmatically:
use tx2_link::init_debug_mode;
fn main() {
// Reads TX2_DEBUG and TX2_TRACE environment variables
init_debug_mode();
// Your code here - debug logging happens automatically
}
Binary protocols like MessagePack and Bincode are efficient but opaque. Without debug mode, inspecting what's being sent over the wire requires:
With debug mode, you get instant visibility into:
This makes tx2-link extremely "vibe coding friendly" - you can see exactly what's happening without writing debugging code.
serde - Serialization frameworkrmp-serde - MessagePack formatbincode - Bincode formatserde_json - JSON formattokio - Async runtimebytes - Efficient byte buffersahash - Fast hashingthiserror - Error handlingMIT
Contributions are welcome! This is part of the broader TX-2 project for building isomorphic applications with a unified world model.