Skip to main content

JavaScript Library

@cluvio/embed embeds a sharing link in your page. It creates the iframe, sets its height from embedSize, and turns the actions and events into typed methods and listeners.

On a link with Require Sharing Secret turned on, it is the browser half of the embed: it renews the sharing secret before it expires. Signing stays on your server. The library never sees the Embed Secret: it asks your server for each sharing secret through a function you give it.

npm install @cluvio/embed

It is one ES module with no dependencies and its own TypeScript types. Its source, under the MIT license, is cluvio/cluvio-embed on GitHub.

Without a bundler, load it in a <script type="module">. Copy dist/index.js into your own static files, or load it from a CDN that serves npm packages, with the exact version in the URL:

<div id="dashboard"></div>
<script type="module">
import { CluvioEmbed } from 'https://cdn.jsdelivr.net/npm/@cluvio/embed@1.0.0/dist/index.js';

await CluvioEmbed.mount({
container: document.getElementById('dashboard'),
dashboardId: 'wm73-peg8-y0qv',
sharingToken: 'fc77dd26-1b9d-417d-9489-518bc4ab7431',
});
</script>

On a page where users are signed in, prefer your own copy: a script from a CDN runs with the same access to the page as your own code.

Embedding a dashboard​

On a link that doesn't require a sharing secret, give the library the element to add the iframe to, and the link's dashboard ID and sharing token:

import { CluvioEmbed } from '@cluvio/embed';

const embed = await CluvioEmbed.mount({
container: document.getElementById('dashboard'),
dashboardId: 'wm73-peg8-y0qv',
sharingToken: 'fc77dd26-1b9d-417d-9489-518bc4ab7431',
});

On a link that requires one, your server needs an endpoint that signs a sharing secret for the signed-in user, as in Creating Sharing Secrets, and returns it. Sign iat as well as exp: the library takes the secret's lifetime from the two. Include keep_open_after_expiration: true, so the dashboard stays on screen while a renewal is on its way.

The page then mounts the dashboard with a fetchToken function that calls that endpoint:

import { CluvioEmbed } from '@cluvio/embed';

const embed = await CluvioEmbed.mount({
container: document.getElementById('dashboard'),
dashboardId: 'wm73-peg8-y0qv',
sharingToken: 'fc77dd26-1b9d-417d-9489-518bc4ab7431',
// Your own endpoint, which signs a sharing secret for the signed-in user
fetchToken: async (signal) => {
const response = await fetch('/cluvio/sharing-secret', { method: 'POST', signal });
if (!response.ok) throw new Error(`Sharing secret request failed: ${response.status}`);
return (await response.json()).sharingSecret;
},
locale: 'de',
parameters: { timerange: '12_full_week', aggregation: 'week' },
});

mount calls fetchToken for the first sharing secret, then adds the iframe to the container. The dashboard ID and sharing token are not secret: on a link that requires a sharing secret, they open nothing without one.

OptionWhat it does
containerThe element the iframe is added to.
dashboardId, sharingTokenThe sharing link's dashboard and token.
dashboardsUrlWhere the sharing link is served. Default https://dashboards.cluvio.com, the same in every region. Set it only when your dashboards are served from your own domain, as on a white-labeled data product.
fetchToken(signal)Returns a new sharing secret from your server. Called for the first one, before each one expires, and when the dashboard sends secretExpired. Leave it out on a link that doesn't require a sharing secret.
renewAtWhen to renew, as a fraction of the secret's lifetime, between 0 and 1. Default 0.8.
localeThe dashboard's language and formats, such as de or pt-PT.
currencyAn ISO 4217 code such as EUR. The library sets the currency symbol, and which side of the number it goes on, the way locale writes it.
darkModetrue, false, or 'auto' to follow the viewer's system setting.
parametersThe filter values the dashboard opens with: timerange, aggregation and filters, as in changeParameters.
expandFiltersOpens the dashboard's filter bar, on a link that shows one.
drillEventsAdds enableDrillEvents to the link, so the dashboard sends drillDown and drillSelect. Without it, it sends neither.
autoHeightSets the iframe's height from embedSize. Default true.
titleThe iframe's accessible name. Default Dashboard.
signalAn AbortSignal. Aborting it removes the embed, or cancels a mount still waiting for its first sharing secret.

Renewal​

The library renews the sharing secret at 80% of its lifetime (renewAt), and posts the new one to the dashboard with updateSharingSecret. The lifetime comes from the secret's iat and exp, and counts from when the browser asked for the secret, by the browser's clock. A clock a few minutes off, or a secret that took a while to arrive, still renews on time.

It also renews when the dashboard sends secretExpired, and as soon as the page is visible again if a renewal fell due while timers weren't running, such as on a laptop that was asleep. Reasons that arrive together share one request. A failed request is retried after 2, 5 and 15 seconds, then every 30 seconds, and each failure is reported as an error event. A secret that has already expired when it arrives, or is signed for another sharing link, counts as a failed request.

embed.renew() renews at once, for example after something in your application changed what the user may see. embed.token is the sharing secret in use, with its decoded claims and when it expires and renews.

Without fetchToken there is nothing to renew: embed.token is undefined and renew() fails.

Actions​

Each action is a method. Actions called before the dashboard has loaded are sent once it has, except navigate, which then loads the target link in the iframe's place.

MethodAction
changeParameters(parameters)changeParameters
showReport(reportId)showReport
showDashboard()showDashboard
setDarkMode(darkMode)updateSettings
downloadPdf(reportId?)downloadPdf
downloadImage(reportId?)downloadImage
downloadExcel()downloadExcel
refresh()refresh
renew()updateSharingSecret, with a new sharing secret from fetchToken
navigate(target)navigate, with a sharing secret for the target link. See Opening another dashboard.
destroy()Removes the iframe and stops its timers, listeners and requests.
document.getElementById('period').addEventListener('change', (event) => {
embed.changeParameters({ timerange: event.target.value });
});

Opening another dashboard​

navigate opens another sharing link in the same iframe, typically to answer a drillDown event. A sharing secret opens only the link it was signed for, so navigate takes the target link's own fetchToken. It fetches the first secret with it, refuses one signed for another link, and renews with it from then on. For a target link that doesn't require a sharing secret, leave out fetchToken; the library then stops renewing. If navigate is called again before the first call's sharing secret arrives, the later call wins, and the earlier one rejects with an AbortError.

parameters opens the target with filter values, in the form changeParameters takes: usually the drill-down's own values.

embed.on('drillDown', async ({ dashboardId, filters }) => {
await embed.navigate({
dashboardId,
sharingToken: sharingTokenFor(dashboardId), // the target dashboard's sharing link
// Your own endpoint, which signs a sharing secret for that link
fetchToken: async (signal) => {
const response = await fetch('/cluvio/drill-secret', {
method: 'POST',
body: JSON.stringify({ dashboardId, filters }),
signal,
});
return (await response.json()).sharingSecret;
},
// The drill-down's values, as { variableName: value }
parameters: {
filters: Object.fromEntries(filters.map((filter) => [filter.variableName, filter.value])),
},
});
});

Filter values from the page are limited by the secret's fixed_parameters like any other. To lock the target to the drill-down's values, your server signs them into fixed_parameters instead. Check them against what the signed-in user may see before you sign them, as you would any value the browser sends.

Events​

embed.on(event, listener) listens to an event and returns a function that stops listening. The dashboard's events arrive with their params:

embed.on('parametersChanged', ({ parameters }) => {
document.getElementById('period').value = parameters.timerange;
});

The library adds four of its own:

EventParamsSent when
tokenThe sharing secret's state, as embed.tokenA new sharing secret is in use
errorerror, reason, retryInFetching a sharing secret failed; it is retried after retryIn milliseconds. Without retryIn, the dashboard sent secretExpired to an embed with no fetchToken.
receivedevent, paramsAny message arrives from the dashboard
sentaction, paramsAny action is sent to the dashboard

received and sent together show everything that passes between your page and the dashboard, which is what to log while you build the page.

The library accepts messages only from its own iframe, from the origin of dashboardsUrl (https://dashboards.cluvio.com unless you set it), and posts its actions to that origin only.