Lattice Grid Buy a licence

api reference

The mock socket

A scripted feed with no backend behind it, for building and demonstrating a live screen.

API reference › The mock socket

All 13 pages Everything on one page → Developer guide →

The mock socket

modules/mock-socket is a serverless stand-in for a live WebSocket feed, for building and demonstrating a real-time UI with no backend. MockWebSocket presents the same surface as the browser's WebSocket - the same readyState and state constants, the same onopen, onmessage, onclose and onerror, addEventListener, send and close - so the code that reads it does not change when it is swapped for a real one. It fires an initial snapshot the moment it opens, then a stream of deltas on a timer, all from a generator you hand it. It is a dev and test utility: optional, imports nothing from the grid, and is never pulled into the core bundle. It pairs naturally with the data router (one mock stream, partitioned to many grids), but depends on it no more than a real socket does.

import { MockWebSocket, opsFeed } from '@toclocoinc/lattice-grid/modules/mock-socket';

const socket = new MockWebSocket({ feed: opsFeed({ seed: 7 }) });
socket.onmessage = (event) => {
  const message = JSON.parse(event.data);
  if (message.kind === 'snapshot') router.load(message.rows);
  else router.apply(message.changes);
};

// Going live is the one line that changes; everything above stays as written:
const socket = new WebSocket('wss://example.com/ops');

The swap is literally one line. Both sockets frame their messages the same way, so the reader parses event.data and switches on kind either way. The feed is seedable and deterministic: the shipped generators carry their own seed and the timing jitter is seeded too, so the same inputs replay the same stream - which is what lets a tutorial and its runnable example show the same thing every time, and what lets a test assert on an exact stream rather than a plausible one. Bring your own generator: a feed is any iterator that yields { kind: 'snapshot', rows } first and then { kind: 'delta', changes } forever - a plain generator function is the easiest form - and rng(seed) is exported so a custom feed can be seeded the same way the shipped ones are.

MemberDescription
new MockWebSocket({ feed, rate?, jitter?, seed?, snapshotDelay?, pauseWhenHidden?, url? })Open a mock socket driven by feed (a generator: snapshot first, then deltas). rate is the ms between deltas (default 1000); jitter a random plus-or-minus ms per gap (default 0); seed seeds that jitter (default 1); snapshotDelay the ms before it opens (default 60); pauseWhenHidden stops the feed while the tab is in the background (default true); url a cosmetic address so socket.url reads like the real thing.
onopen / onmessage / onclose / onerrorThe WebSocket handlers. onmessage receives an event whose data is the JSON-framed FeedMessage; a feed that ends closes the socket cleanly; a feed that throws surfaces as an error event, not an uncaught throw.
addEventListener / removeEventListenerThe EventTarget surface, alongside the on* handlers - both receive every event.
send(data?)Accepted and ignored: there is nothing upstream, so a page that calls send runs unchanged.
close()Close the socket and stop the feed, emitting a clean close.
pause() / resume()Hold the feed and continue it while the socket stays open - a demo and test affordance beyond the WebSocket surface.
opsFeed({ seed?, orders?, shipments?, incidents?, batch? })A mixed operations feed - orders, shipments and incidents across three regions plus a throughput rollup - the kind the data router partitions across several grids and a chart from one source. Yields a snapshot, then deltas forever.
priceFeed({ seed?, symbols?, move?, batch?, spread? })A market-data feed: instruments whose prices random-walk each tick, each record carrying type: 'price', symbol, last, chg and a bid/ask straddling the last. Yields a snapshot, then deltas forever.
rng(seed)A small seeded pseudo-random generator (mulberry32), so a custom feed can be seeded the same way the shipped ones are: the same seed yields the same sequence.

Every record on the shipped feeds carries a type, the property the data router partitions on, and an id (or symbol), its row key - so a mock feed drops straight into a routed screen. The module is plain JavaScript and timers: no dependencies, no eval, safe to paste into a page or a sandbox.

Type reference

Generated from the type declarations, so it always matches the release. Each surface lists its properties, its methods and the events it raises as three tables; an option or value type lists its members once.

The mock socket

FeedMessage

PropertyTypeDescription
kindFeedMessageKind 'snapshot' | 'delta'Whether this message opens the feed with a full set of rows (`'snapshot'`, which the feed yields first) or carries changes to apply to what is already there (`'delta'`, every message after it).
rowsFeedRow[]Present on a snapshot: the full opening set of rows. (optional)
changesFeedChange[]Present on a delta: the changes to apply. (optional)