Nitride.xyz
Receivers

Node.js receiver

Receive the stream with Node's built-in dgram module. No packages to install. It runs on Node 20 or later and on Bun.

Run it#

Save the script as receiver.mjs and start it with your UDP port as the first argument. A second argument is optional: an address to accept exclusively. Add it only after delivery is active: support gives you the source address then, so while you verify there is nothing to filter on. Datagrams made of printable characters, which is what the token is, appear as text from lines.

receiver.mjs
1// receiver.mjs: node receiver.mjs 9000 [source-ip]
2import dgram from "node:dgram";
3
4const PORT = Number(process.argv[2] ?? 9000);
5const SOURCE = process.argv[3]; // optional: only accept this sender
6const COMMON_HEADER = 83;
7
8const sock = dgram.createSocket({ type: "udp4", recvBufferSize: 8 * 1024 * 1024 });
9
10let packets = 0;
11let last = { slot: 0n, index: 0, fec: 0 };
12
13sock.on("message", (msg, rinfo) => {
14 if (SOURCE && rinfo.address !== SOURCE) return;
15 if (msg.length > 0 && msg.every((b) => b === 9 || b === 10 || b === 13 || (b >= 32 && b < 127))) {
16 console.log(`text from ${rinfo.address}: ${msg.toString("ascii").trim()}`);
17 return;
18 }
19 if (msg.length < COMMON_HEADER) return;
20 packets++;
21 last = {
22 slot: msg.readBigUInt64LE(65),
23 index: msg.readUInt32LE(73),
24 fec: msg.readUInt32LE(79),
25 };
26});
27
28setInterval(() => {
29 console.log(`packets=${packets} slot=${last.slot} index=${last.index} fec=${last.fec}`);
30 packets = 0;
31}, 1000);
32
33sock.bind(PORT, "0.0.0.0", () => console.log(`listening on udp://0.0.0.0:${PORT}`));
terminal
node receiver.mjs 9000

What it prints#

Once a second, the number of datagrams received and the header of the latest one. The offsets are the shred’s common header: slot at byte 65, index at 73 and FEC set index at 79, all little-endian. The full layout is on the Rust receiver page.

Limits of a JavaScript receiver#

The event loop handles one datagram per callback, as the Python receiver does one per call; the Go receiver is a compiled alternative with the same output. That is fine for confirming delivery and for light processing. Under bursts, a busy event loop lets the socket buffer fill and the kernel drops what doesn’t fit.

Next#

Raise the kernel’s buffer limit so recvBufferSize takes effect: see receive-side requirements. The quickstart covers the steps before this one, and the command-line checks help when packets go missing.