Write a remote vertex, then read it (TypeScript)

LibtracerClient runs over any transport with send() and onFrame(). Each write and read is one FWD frame out and one FWD{REPLY} back.

The node on the other end is stand-in-node.mjs, an in-memory stand-in. The TypeScript packages have no node of their own, and the stand-in lets the example run with no C++ build. Against a real node the client code is unchanged.

What to notice

  • The transport is injected. The client never opens a socket. Here it is handed an in-memory link, and the dial example hands it a WebSocket without changing the client code.

  • A read returns the last value written. Nothing is queued, and a second write replaces the first.

  • Replies are matched by path, not by order. The reply’s src echoes the request’s dst, which is how the client pairs them without a correlation id.

Source

 1// SPDX-License-Identifier: Apache-2.0
 2// SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
 3
 4/**
 5 * @file
 6 * @brief One concept: write a remote vertex, then read it back. Each call is one FWD
 7 * frame out and one FWD{REPLY} back, matched by the path it answers for.
 8 *
 9 * Run: `node examples/write-read.mjs` (from `bindings/typescript/`, after `npm run build`).
10 */
11
12import assert from 'node:assert/strict';
13import { LibtracerClient, encodeValue } from '@avatarsd-llc/libtracer-client';
14import { standInNode } from './stand-in-node.mjs';
15
16// The client takes any transport with send() + onFrame(); here, an in-memory link.
17const client = new LibtracerClient(standInNode().loopback());
18
19// A write carries a complete VALUE TLV; it resolves when the node acks.
20const reading = Uint8Array.of(0xfc, 0x53, 0x00, 0x00); // 21500, u32 little-endian
21await client.write('/sensor/temp', encodeValue(reading));
22console.log('wrote /sensor/temp');
23
24// A read resolves to the decoded VALUE: the last value written, nothing queued.
25const value = await client.read('/sensor/temp');
26assert.deepEqual(value.payload, reading);
27console.log(`read /sensor/temp -> ${Buffer.from(value.payload).readUInt32LE(0)}`);
28
29// Last writer wins: a second write replaces the first.
30await client.write('/sensor/temp', encodeValue(Uint8Array.of(0, 0, 0, 0)));
31assert.deepEqual((await client.read('/sensor/temp')).payload, Uint8Array.of(0, 0, 0, 0));
32console.log('second write replaced the first');

Run it from bindings/typescript/, after npm ci && npm run build:

$ node examples/write-read.mjs

See also: read and write (C++) · Rust: the WRITE frame.