Realtime (@swelte/plus)

Rooms + sockets on one hub — beyond-SvelteKit, optional package. Ships inside @swelte/plus/db (same install, same dbPlugin): the hub is the reason sockets live here and not in core. Core stays parity-only; plus owns the backend.emit hub @[sse] and @[ws] ride.

install + dbPlugin
npm install @swelte/plus

Rooms — cross-request broadcast

Any verb/@[rpc] publishes with backend.emit(room, msg) (returns the delivered count); a room-mode @[sse] fn subscribes and streams live hub messages. Single-instance best-effort (same tier as default process-local cron); backpressure-safe (cap-1 buffer, slow subscribers drop, publishers never stall); harness-owned subscriptions (auto-reaped on disconnect — return from the fn to leave).

rooms: emit + room SSE
// cross-request broadcast: any RPC publishes, room SSE streams
@[rpc]
pub fn say(args SayIn) int {
	return backend.emit(args.room, args.msg) // delivered count
}

@[sse]
pub fn stream(req SwelteRequest, sub fn (string) chan string, emit fn (string) bool) {
	room := req.query['room'] or { 'lobby' }
	ch := sub(room) // harness owns it; reaped when this fn returns
	for {
		select {
			m := <-ch {
				if !emit(m) {
					return
				}
			}
			30 * time.second {
				if !emit('__ping__') {
					return
				} // idle heartbeat, client ignores
			}
		}
	}
}

Sockets — first-class `@[ws]` rooms

One socket fn per file on the same hub: backend.emit(room, msg) publishes cross-request, the harness injects the sub room-subscribe closure, the inbound recv channel, and the emit frame emitter. Echo round-trips client frames; rooms fan out hub messages to every subscriber. TS stubs are URL builders — the browser opens a native WebSocket itself. Non-upgrade GETs on the same path fall through to dispatch.

echo: recv → emit
// echo socket: whatever the client sends, the server sends back
@[ws]
pub fn ws_echo(req SwelteRequest, sub fn (string) chan string, recv chan string, emit fn (string) bool) {
	_ = req
	_ = sub
	for {
		msg := <-recv or { return } // client gone → leave
		if !emit(msg) {
			return
		}
	}
}
rooms: sub + emit on the hub
// room socket: joins ?room= (default lobby); hub messages stream out
import backend
import time

pub struct WsSayIn {
pub:
	room string
	msg  string
}

@[rpc]
pub fn ws_say(args WsSayIn) int {
	return backend.emit(args.room, args.msg) // delivered count
}

@[ws]
pub fn ws_rooms(req SwelteRequest, sub fn (string) chan string, recv chan string, emit fn (string) bool) {
	room := req.query['room'] or { 'lobby' }
	ch := sub(room) // harness owns it; reaped when this fn returns
	for {
		select {
			m := <-ch {
				if !emit(m) {
					return
				}
			}
			n := <-recv {
				_ = n // drained; echo variant sends it back
			}
			30 * time.second {
				if !emit('__ping__') {
					return
				} // idle heartbeat, client ignores
			}
		}
	}
}
browser: new WebSocket(url)
<script lang="ts">
	import { ws_echo, ws_rooms } from '#routes/api/ws-echo.v'
	import { onMount } from 'svelte'

	// TS stubs are URL builders (same route + query as fetch stubs).
	// The browser opens a native WebSocket itself.
	let log = $state<string[]>([])
	onMount(() => {
		const echo = new WebSocket(ws_echo())
		echo.onmessage = (e) => log.push('echo: ' + e.data)
		echo.onopen = () => echo.send('hello')
		const rooms = new WebSocket(ws_rooms({ room: 'lobby' }))
		rooms.onmessage = (e) => {
			if (e.data !== '__ping__') log.push('room: ' + e.data)
		}
		return () => {
			echo.close()
			rooms.close()
		}
	})
</script>

Native POST streaming and cleanup

swelte_stream(headers, producer) writes native POST response bytes with bounded buffering. Producers can frame SSE events and deliver them before completion. Request guards also cover streams and socket takeover. Disconnects, slow-reader timeouts and idle SSE checks release subscribers; graceful shutdown drains active work before running cleanup hooks. Replay and durable message storage belong to the application.