Packages
@surfjs/svelte
Svelte stores and actions for Surf โ local commands, reactive state, and Surf Live
@surfjs/svelte
Svelte integration for Surf, wrapping @surfjs/web. Register local command handlers, sync state via stores, and connect to Surf Live. Supports Svelte 4 and 5.
Installation#
npm install @surfjs/svelte@surfjs/web is included as a dependency โ no separate install needed.
surfCommands
Register local handlers that execute in the browser:
<script> import { surfCommands } from '@surfjs/svelte'ย surfCommands({ 'theme.toggle': { mode: 'local', run: () => { document.documentElement.classList.toggle('dark') return { ok: true } } }, 'canvas.clear': { mode: 'local', run: () => { canvasStore.clear() return { ok: true } } } })</script>Handlers are registered immediately and cleaned up via onDestroy.
surfState
Reactive Svelte store synced from Surf Live events:
<script> import { surfState } from '@surfjs/svelte'ย const metrics = surfState('metrics', { users: 0, events: 0 })</script>ย <div>Online: {$metrics.users}</div>Returns a Svelte writable store that auto-updates when the server sends surf:state or surf:patch events for the given key.
surfExecute
Convenience wrapper around window.surf.execute():
<script> import { surfExecute } from '@surfjs/svelte'ย async function search(query) { const result = await surfExecute('search', { query }) console.log(result) }</script>Context API#
For apps using Surf Live, set up the provider context:
<!-- App.svelte --><script> import { createSurfProvider, setSurfContext } from '@surfjs/svelte'ย const provider = createSurfProvider({ url: 'wss://myapp.com/surf/ws', endpoint: 'https://myapp.com', })ย setSurfContext(provider)</script>ย <slot />Then access it in child components:
<script> import { getSurfContext } from '@surfjs/svelte'ย const { execute, connected } = getSurfContext()</script>| Option | Type | Description |
|--------|------|-------------|
| url | string | WebSocket URL for Surf Live |
| endpoint | string? | Server URL for HTTP fallback |
| auth | string? | Auth token |
| channels | string[]? | Surf Live channels to subscribe to |