Download Game! Currently 80 players and visitors. Last logged in:SodroNosutestCilwandHugoo

Blitzer's Blog >> 72080

Back to blogs index
Posted: 05 Sep 2026 15:30 [ permalink ]
Here are the exact JavaScript additions you need to bridge your local VM
objects with your WebSocket relay.

By utilizing the `RAW_TARGET` symbol exposed by your proxy in `10_mesh.js`, we
can perform a "stealth write" directly to the underlying object. This bypasses
the Proxy's `set` trap, completely neutralizing the risk of an infinite
broadcast loop where an incoming network change triggers an outbound mesh
sync.

You can append this code directly to the bottom of the `<script>` block in
your `template.html`, right after the DOM REPL logic.

### The Mesh Bridge Implementation

```javascript
/ ==========================================
/ 6. MESH BRIDGE: INBOX & OUTBOX
/ ==========================================

/ --- OUTBOX SWEEPER ---
/ Polls the VM's outbox array every 50ms for local mutations
setInterval(() => {
    const rt = window.__rt;
    
    // Check if runtime exists, outbox has pending items, and socket is ready
    if (rt && rt.__OUTBOX && rt.__OUTBOX.length > 0 && dmzSocket.readyState
=== WebSocket.OPEN) {
        
        // Drain the queue chronologically (FIFO)
        while (rt.__OUTBOX.length > 0) {
            const payload = rt.__OUTBOX.shift(); 
            dmzSocket.send(JSON.stringify(payload));
        }
    }
}, 50);

/ --- INBOX RECEIVER ---
/ Listens for CRDT_MUTATION payloads and applies them safely
dmzSocket.addEventListener('message', (event) => {
    try {
        const msg = JSON.parse(event.data);
        const rt = window.__rt;
        
        // Handle incoming mesh state mutations
        if (msg.type === 'CRDT_MUTATION' && rt && rt.master_objects) {
            const targetProxy = rt.master_objects[msg.object_id];
            
            if (targetProxy) {
                // Fetch the bypass symbol mapped in 10_mesh.js
                const RAW_TARGET = Symbol.for("RAW_TARGET");
                
                // If the proxy exposes the base object, apply the change
directly 
                if (targetProxy[RAW_TARGET]) {
                    targetProxy[RAW_TARGET][msg.property] = msg.value;
                }
            }
        }
    } catch (e) {
        console.error("[DMZ] Mesh Inbox Error:", e);
    }
});

```

### Key Technical Notes:

* **Event Listener Appending**: Rather than rewriting your existing
`dmzSocket.onmessage` handler which processes your `VFS_WRITE` and
`VFS_DELETE` logic, using `addEventListener` cleanly separates the mesh object
sync logic from the Virtual File System sync logic.


* **FIFO Queue Drainage**: Using `.shift()` ensures your mutations are
processed in the exact chronological order they were pushed to `__OUTBOX` by
your VM.


* **Defensive Checks**: The receiver strictly verifies that `window.__rt` and
`window.__rt.master_objects` exist before attempting memory allocation,
preventing race condition crashes if network data arrives while the LPC
WebAssembly VM is still booting.



Do you need to implement a mechanism (like vector clocks or Lamport
timestamps) in this receiver to resolve race conditions if two clients mutate
the exact same property at the same time?