Server side
eventStream calls your function every every milliseconds and sends what it returns. It answers with a Web Response, so it runs anywhere that speaks them. It stops when the visitor leaves.
Next.js
// app/api/cpu/route.ts
import os from 'node:os';
import { eventStream } from 'nervure/server';
export const dynamic = 'force-dynamic';
export const GET = (request: Request) =>
eventStream(() => ({
time: new Date().toLocaleTimeString('en-GB'),
load: Math.round((os.loadavg()[0] / os.cpus().length) * 100),
}), { every: 1000, signal: request.signal });In the browser: events('/api/cpu').
A paid API, with the key kept secret
export const GET = (request: Request) =>
eventStream(async (signal) => {
const r = await fetch('https://api.example.com/quote?symbol=ACME', { headers: { 'X-Api-Key': process.env.QUOTE_KEY! }, signal });
if (!r.ok) throw new Error(`HTTP ${r.status}`); // this message is skipped
return r.json();
}, { every: 5000, signal: request.signal });Each visitor opens its own stream. With many visitors, read the API once, keep the answer, and send it to all.
Other servers
app.get('/stream', (c) => eventStream(read, { every: 1000, signal: c.req.raw.signal }));export const GET = ({ request }) => eventStream(read, { every: 1000, signal: request.signal });export default defineEventHandler(() => eventStream(read, { every: 1000 }));export const GET = ({ request }) => eventStream(read, { every: 1000, signal: request.signal });PHP, Python, Go or any other language: send text/event-stream with one data: {…} line of JSON followed by an empty line, and events() reads it.
SVG on the server
nervure/render draws a chart without a browser and returns a standalone SVG string. It runs in Node, Deno, Bun and edge workers, with no dependency: a small document of its own stands in for the DOM while the chart is drawn, then goes away.
import { renderToSVG, toMarkdown } from 'nervure/render';
const svg = renderToSVG(
{ type: 'xy', title: 'Orders', categories: ['Jan', 'Feb', 'Mar'], series: [{ name: 'Orders', kind: 'bar', data: [120, 180, 150] }] },
{ width: 640, height: 320, theme: 'light', locale: 'en-GB', format: { compact: true } },
);
const table = toMarkdown(spec); // the data as a Markdown table, under the title| Option | Does |
|---|---|
width, height | pixels, 600 × 320 by default |
theme | light by default, dark, or your own colours |
locale | how numbers and dates are written |
format | how figures are written when the spec has no format |
title, subtitle, caption, background, padding, figma | as in Export |
Every chart type renders, maps included when you pass their GeoJSON or TopoJSON. Nothing moves and nothing reacts to the pointer. Text widths are estimated, since there is no font to measure: a long label may be cut a little sooner or later than in a page. A scatter plot with many points is drawn with SVG circles, as there is no canvas.
A Node script
// charts.mjs: writes one file for each chart of the report
import { writeFileSync } from 'node:fs';
import { renderToSVG } from 'nervure/render';
for (const [name, spec] of Object.entries(report)) writeFileSync(`out/${name}.svg`, renderToSVG(spec, { width: 800, height: 400 }));Symfony, Laravel and other back ends
PHP, Python or Ruby call a small Node service that turns a spec into an SVG. Keep it on the inside of your network.
// chart-service.mjs: node chart-service.mjs
import { createServer } from 'node:http';
import { renderToSVG } from 'nervure/render';
createServer(async (request, response) => {
let body = '';
for await (const part of request) body += part;
try {
const { spec, options } = JSON.parse(body);
response.writeHead(200, { 'content-type': 'image/svg+xml' }).end(renderToSVG(spec, options));
} catch (e) {
response.writeHead(400).end(String(e));
}
}).listen(3020, '127.0.0.1');$svg = $this->httpClient->request('POST', 'http://127.0.0.1:3020', [
'json' => ['spec' => $spec, 'options' => ['width' => 640, 'height' => 320]],
])->getContent();$svg = Http::post('http://127.0.0.1:3020', [
'spec' => $spec, 'options' => ['width' => 640, 'height' => 320],
])->body();A spec sent as JSON has no functions: write figures with format options ({ currency: 'EUR', compact: true }) instead of a function.
Images in an e-mail
Most e-mail programs do not show SVG. Turn the SVG into a PNG on the server, with a tool such as @resvg/resvg-js or sharp, and attach it or link it. Give it a background, since a mail can be dark.
import { Resvg } from '@resvg/resvg-js';
const png = new Resvg(renderToSVG(spec, { background: '#ffffff' }), { fitTo: { mode: 'width', value: 1280 } }).render().asPng();PDF
Put the SVG in the HTML page that your PDF tool prints (Chromium, Gotenberg, WeasyPrint), or give the file to a tool that reads SVG (Typst, Inkscape, svg-to-pdfkit). The text stays text, so it stays sharp and can be searched.