Direct subscriptions recipes
Render a live feed with your framework's component binding.
Server-sent events · Vite · Svelte
Before you start
Section titled “Before you start”Start with an existing Vite · Svelte app. Keep its renderer, routes and plugins. These files add one live view; Spinetab does not create your API.
Your endpoint supplies complete { "open": 12 } values: JSON in each data: field of /api/queue/events, served as text/event-stream.
The examples use same-origin URLs and declare
anonymous: true: no token is supplied, although cookies
still flow. For a separate API origin, use its URL and configure CORS or
your application's proxy. For private data, add
credentials and user scopes;
never put a secret in these files.
1. Install
Section titled “1. Install”pnpm add spinetabKeep your framework's existing dependencies. See compatible versions if upgrading an older app.
2. Configure your app
Section titled “2. Configure your app”Merge this addition into your configuration; preserve existing plugins and options. Restart the dev server afterwards. The plugin bundles the worker and discovers the adapters imported below.
import { svelte } from "@sveltejs/vite-plugin-svelte";import { spinetab } from "spinetab/vite";import { defineConfig } from "vite";
export default defineConfig({ plugins: [svelte(), spinetab()] });3. Connect and render
Section titled “3. Connect and render”Save these files together in src/recipe/.
reconcile: "latest" fits this full-state contract: the next complete event restores the displayed value after a gap. Use a refresh/merge policy for deltas or partial GraphQL results. Reconnecting alone does not reconstruct missed state.
import { createSpinetab } from "spinetab";
export const spinetab = createSpinetab({ anonymous: true });export type Queue = { open: number };export type QueueView = { open?: number; problem?: string };import { sse } from "spinetab/sse";import type { Queue, QueueView } from "./queue-types";
export const queueSource = sse<Queue>("/api/queue/events");export const selectQueue = (queue: Queue): QueueView => queue;<script lang="ts">import { bindClient } from "spinetab/svelte";import { spinetab } from "./live";import { queueSource, selectQueue } from "./source";
const { liveStore } = bindClient(spinetab);const queue = liveStore(queueSource, { map: selectQueue, reconcile: "latest" });</script>
{#if $queue.error || $queue.data?.problem} <p role="alert">{$queue.error?.message ?? $queue.data?.problem}</p>{:else if $queue.data?.open === undefined} <p role="status">Loading queue…</p>{:else} <p><output>{$queue.data.open}</output> open</p>{/if}4. Mount the view
Section titled “4. Mount the view”<script lang="ts">import Recipe from "./recipe/Recipe.svelte";</script><Recipe />Your existing entry point mounts App as usual. Keep its renderer plugin; no additional application provider is required beyond those shown.
Check the result
Section titled “Check the result”Open the page in two tabs of the same browser profile. Both should show advancing queue values. Matching subscriptions share upstream work when SharedWorker is available; fallback runs independently in each tab. Remove one view and the other should keep updating. Removing the last view releases its subscription; connection closure can follow the adapter's idle delay.