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 Server npm install @apinator/client 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.
npm install @apinator/server @apinator/client See your first event (no backend)
No server code yet: subscribe to the public channel my-channel (public channels need no auth endpoint) with your app key and cluster (us or eu) from the dashboard, then click the Send test event button on your app's Overview page in the dashboard. The event prints in your console. Open the dashboard
<!-- src/routes/+page.svelte -->
<script lang="ts">
import { onMount } from 'svelte'
import { Apinator } from '@apinator/client'
onMount(() => {
const client = new Apinator({ appKey: 'YOUR_APP_KEY', cluster: 'us' }).connect()
client.subscribe('my-channel').bind('my-event', (data) => console.log('Received:', data))
return () => { client.disconnect() }
})
script> 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.
// src/routes/api/realtime/auth/+server.ts
import { json } from '@sveltejs/kit'
import { Apinator } from '@apinator/server'
import { REALTIME_APP_ID, REALTIME_KEY, REALTIME_SECRET, REALTIME_CLUSTER } from '$env/static/private'
const realtime = new Apinator({
appId: REALTIME_APP_ID,
key: REALTIME_KEY,
secret: REALTIME_SECRET,
cluster: REALTIME_CLUSTER
})
export async function POST({ request }) {
const { socket_id, channel_name } = await request.json()
const auth = realtime.authenticateChannel(socket_id, channel_name)
return json(auth)
} 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.
<script lang="ts">
import { onMount } from 'svelte'
import { Apinator } from '@apinator/client'
let messages = $state([])
const client = new Apinator({
appKey: 'your-app-key',
cluster: 'us',
authEndpoint: '/api/realtime/auth'
})
onMount(() => {
const ch = client.connect().subscribe('private-chat')
ch.bind('message', (d) => messages = [...messages, d])
return () => client.disconnect()
});
script> Trigger from server load or actions
Publish events from SvelteKit form actions, server load functions, or API routes.
// src/routes/api/send/+server.ts
export async function POST({ request }) {
const data = await request.json()
await realtime.trigger({
name: 'message',
channel: 'private-chat',
data: JSON.stringify(data)
})
return json({ ok: true })
} 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. Typed client options, channels, and API responses.
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.
import { json } from '@sveltejs/kit'
import { Apinator } from '@apinator/server'
import { REALTIME_APP_ID, REALTIME_KEY, REALTIME_SECRET, REALTIME_CLUSTER } from '$env/static/private'
const realtime = new Apinator({
appId: REALTIME_APP_ID, key: REALTIME_KEY, secret: REALTIME_SECRET,
cluster: REALTIME_CLUSTER
})
export async function POST({ request }) {
const { socket_id, channel_name } = await request.json()
return json(realtime.authenticateChannel(socket_id, channel_name))
}Related Docs
Read the core WebSocket guides
Related Use Cases
Explore realtime use cases
From the blog
WebSocket Protocol Explained
A practical guide to the WebSocket protocol — how connections are established, how frames are structured, what opcodes do, and what all of this means when you're building realtime apps.
How the WebSocket Handshake Works
The WebSocket connection starts as HTTP and upgrades to a persistent socket. Here's exactly what happens during the handshake — headers, keys, and all.
Presence Channels Explained
Presence channels let you track who's online in real time — showing typing indicators, viewer counts, and live cursors without building your own member tracking.
Start building realtime SvelteKit apps
Completely free. No credit card. No catch.