QuickstartMinimal Example

Quickstart: Minimal Example

This is the fastest way to get a working SP8D channel in your app. For environment requirements, see Installation Requirements →.

See how to set up a minimal SP8D channel for real-time, lock-free messaging in JavaScript and Node.js. Copy-paste code for instant high-performance results.

SP8D Minimal Example: Lock-Free Messaging in JavaScript & Node.js


Minimal Example: Single Producer, Single Consumer

The following is the most basic usage: one producer and one consumer, communicating via a lock-free, bounded channel. For a more complete and production-ready SPSC example, see Basic SPSC Example.

Node.js (TypeScript)

minimal-example.ts
import { createChannel } from "@sp8d/core";
 
const { channel } = createChannel({ slots: 8, slotSize: 64 });
channel.send(new Uint8Array([42, 17, 8]));
const received: Uint8Array | undefined = channel.recv();
console.log("Received:", received); // Uint8Array([42, 17, 8])

If you prefer plain JavaScript, you can use the same code without type annotations.


Browser: Main Thread to Worker (TypeScript)

SP8D works seamlessly in browsers, enabling high-performance communication between the main thread and workers. This example uses two files: one for the main thread, one for the worker. Requires SharedArrayBuffer support in your browser.

main.ts
import { createChannel } from "@sp8d/core";
 
const { channel, buffer } = createChannel({ slots: 8, slotSize: 8 });
 
const worker = new Worker("worker.js");
worker.postMessage(buffer, [buffer]); // Transfer the SharedArrayBuffer
 
// Send a message from main
channel.send(new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));
worker.ts
import { attachChannel } from "@sp8d/core";
 
self.onmessage = (e: MessageEvent<SharedArrayBuffer>) => {
  const channel = attachChannel(e.data);
  const msg: Uint8Array | undefined = channel.recv();
  console.log("Worker received:", msg); // Should log: Uint8Array([1,2,3,4,5,6,7,8])
};

In this setup, the main thread creates the channel and transfers the buffer to the worker. Both sides can now send and receive messages with zero-copy efficiency.

Diagram: Minimal browser/worker data flow: main thread and worker communicate via a shared SP8D channel.


How It Works

This section summarizes the minimal mechanics behind SP8D’s channel operations. For deeper details, see the relevant documentation sections.

  • createChannel: Allocates a shared ring buffer of slots for your messages, with safety and backpressure.
  • send: Producer claims a slot, writes the payload, and marks it as ready.
  • recv: Consumer waits for a ready slot, reads the data, and frees it.
  • All transitions happen atomically—no locks, ever.

Why Use SP8D for Minimal Messaging?

No setup of queues, events, Promise chains, or bounce buffers. Immediate delivery, guaranteed boundedness, lock-free performance. No external dependencies beyond modern JS runtime support. Use channel.info() or channel.stats() to verify state and health.


Try Live

Test SP8D channels interactively in your browser. A live harness is available for experimentation and diagnostics. Open the SP8D Harness →


Where to Go Next