Realtime WebSocket Notifications for SvelteKit
Add WebSocket channels and real-time events to SvelteKit with Svelte 5 runes. Type-safe server SDK for API routes and form actions. Works with $state, onMount, and $env secrets.
npm install @apinator/server npm install @apinator/client SvelteKit's server routes and Svelte 5's reactive runes make real-time WebSocket integration clean and type-safe. Use the server SDK in SvelteKit +server.ts routes to authenticate WebSocket channels and publish events. Use the client SDK with onMount and $state runes to receive live updates in Svelte components — no dedicated WebSocket server needed.
Quick Start
How to add WebSocket notifications to SvelteKit
Add realtime features to your SvelteKit app in a few steps.
Install both SDKs
Add the server SDK for your API routes and the client SDK for Svelte components.
Create a WebSocket auth endpoint
Add a POST +server.ts route that signs WebSocket channel subscription requests using your app secret. Secrets stay in $env/static/private.
Subscribe to a WebSocket channel
Open a persistent WebSocket connection in onMount and subscribe to a channel. Use $state runes for reactive message lists that update the template automatically.
Trigger from server load or actions
Publish events from SvelteKit form actions, server load functions, or API routes.
Why SvelteKit
Built for SvelteKit developers
$env Integration
Use SvelteKit $env/static/private for secrets. Never expose your API secret to the client bundle.
Svelte 5 Runes
Client SDK works naturally with $state and $derived runes. Reactive updates without boilerplate.
Form Actions
Trigger realtime events from SvelteKit form actions for progressive enhancement with live updates.
TypeScript-First
Full TypeScript support in both server and client SDKs. Type-safe events and channel configuration.
How WebSocket channels work in SvelteKit
SvelteKit's architecture separates server and client code cleanly, which maps directly to how Apinator works. Server SDK code — authenticating WebSocket channels, publishing events — lives in +server.ts files or form actions, where it has access to $env/static/private secrets and runs only on the server. Client SDK code — subscribing to channels, binding events — lives in Svelte components where it connects to a persistent WebSocket and updates reactive state.
Svelte 5 runes make this especially clean. A $state([]) array automatically triggers template re-renders when new WebSocket events push items onto it. The onMount and onDestroy pair maps directly to the connect and disconnect lifecycle, preventing memory leaks. For state shared across multiple components, you can declare a $state rune in a shared .svelte.ts module and import it from any component.
Unlike frameworks where you'd need a separate WebSocket server process running alongside your SvelteKit app, Apinator's hosted infrastructure keeps your deployment stateless and serverless-compatible — no additional server process, no configuration.
Full Example
Full Example: Realtime WebSocket Chat with SvelteKit
Complete WebSocket example: a SvelteKit +server.ts auth endpoint, an API route for triggering events, and a Svelte 5 component that renders real-time messages with $state runes.
Related Docs
Read the core WebSocket guides
Related Use Cases
Explore realtime use cases
Start building realtime SvelteKit apps
Completely free. No credit card. No catch.