Skip to content

Live data ​

live feeds a chart from a source. It retries with growing waits after an error, stops while the tab is hidden, and draws at most ten times a second.

Market price ​

ts
import { append, live, poll } from 'nervure/live';

const spec = { type: 'xy', categories: [], series: [{ name: 'Price', data: [], dots: false }] };
const chart = createChart(host, spec);

const feed = live(chart, spec, poll('/api/price', { every: 1000 }), {
  apply: (data, previous) => append(previous, data.time, { Price: data.price }, 60),
  onStatus: (status) => console.log(status),
});

The figures on this page are made up in your browser.

Processor and memory ​

Sources ​

ts
poll('/api/cpu', { every: 2000, init: { headers: { Authorization: `Bearer ${token}` } } }); // an API, every 2 s
poll(async (signal) => (await client.read({ signal })).value, { every: 5000 });              // your own function
events('/api/cpu/stream');                                                                   // server-sent events
socket('wss://example.com/prices', { hello: { subscribe: ['EURUSD'] } });                     // WebSocket
SourceUse it when
pollyou have a plain API; every few seconds is enough
eventsyour server pushes; one way, over plain HTTP, see server side
socketmany messages a second, or an existing WebSocket feed

poll waits for an answer before it counts the next interval, so a slow API is never called twice at once.

Control ​

ts
feed.pause();
feed.resume();
feed.stop();     // call it when the chart goes away
feed.status;     // connecting, live, paused, retrying or stopped
OptionDefaultDoes
applyturns a message into the next spec; null ignores it
throttle100at most one drawing in this time (ms)
retry1 s to 30 swaits after errors; false gives up
pauseWhenHiddentruestops while the tab is hidden
onStatus, onErrorfollow what happens

Long series ​

A year of minutes is more points than pixels. thin keeps the lowest and the highest point of each stretch, so peaks and dips stay visible, and the chart stays quick to draw. The data is untouched: the table and the CSV export keep every point, and zooming in brings the detail back.

js
createChart(el, { type: 'xy', categories, series, thin: true, zoom: true });
// or the most points kept
createChart(el, { type: 'xy', categories, series, thin: 500 });

In React ​

tsx
<Chart spec={spec} style={{ height: 300 }} onReady={(chart) => {
  if (chart) feed.current = live(chart, spec, events('/api/cpu/stream'), { apply });
  else feed.current?.stop();
}} />

Keep keys on your server

A key used in the browser can be read by everyone. Call paid or private APIs from your server, and let the chart read yours.