npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@sourceregistry/sveltekit-websockets

v1.0.9

Published

Typed WebSocket infrastructure for SvelteKit — per-request or continuous connections, full back-end lifecycle, and a reactive Svelte component.

Readme

@sourceregistry/sveltekit-websockets

npm version License CI

Typed WebSocket infrastructure for SvelteKit — per-request or continuous connections, full back-end lifecycle, and a reactive Svelte component for the client.

⚠️ Node-only. This package requires @sveltejs/adapter-node. WebSockets are not supported on serverless or edge adapters.


Features

  • 🔌 Per-request WebSockets tied to SvelteKit form actions
  • 🔁 Persistent WebSocket routes via controller-based routing
  • 🧠 Lifecycle hooks: connect, disconnect, error, destroy
  • 🧩 Reactive Svelte component with auto-connect and snippets for streamed messages
  • ⚙️ Vite plugin that wires up .upgrade() handling in dev and preview
  • 🔐 Ephemeral, single-use connection keys with TTL and automatic cleanup

Installation

npm install @sourceregistry/sveltekit-websockets

Quick start

1. Register the Vite plugin (required)

WebSocket upgrades need a hook into the dev/preview HTTP server. Add the plugin in vite.config.ts:

import {defineConfig} from 'vite';
import {sveltekit} from '@sveltejs/kit/vite';
import {websockets} from '@sourceregistry/sveltekit-websockets/vite';

export default defineConfig({
    plugins: [
        sveltekit(),
        websockets(), // ADD THIS
    ],
    server: {
        ws: {
            port: 5174, // REQUIRED — see note below
        },
    },
});

Without the plugin, .upgrade() requests fail in dev and preview (production builds on adapter-node are patched automatically at build time).

⚠️ Also set server.ws.port to a different port, as shown above. The plugin intercepts every 'upgrade' request on the dev server's HTTP port and closes any that don't match a registered route. Vite's own HMR client connects over a WebSocket on that same port by default, so without a dedicated ws.port it gets killed too — the browser loses its HMR connection, keeps retrying, and the page reloads in a loop.

2. Per-request WebSockets (use())

Bind a single-use WebSocket to a SvelteKit form action. The client gets back a one-time URL that expires automatically.

// src/routes/chat/+page.server.ts
import {websockets} from '@sourceregistry/sveltekit-websockets/server';

export const actions = {
    ws: (event) => ({
        url: websockets.use(event, (socket) => {
            socket.send('Connected!');
            socket.addEventListener('message', (msg) => {
                console.log('Client says:', msg.data);
            });
        }),
    }),
};

3. Persistent WebSockets (continuous())

Register a long-lived endpoint independent of any single request, typically from src/hooks.server.ts:

import {websockets} from '@sourceregistry/sveltekit-websockets/server';

const chat = websockets.continuous('/chat', {useConnectionKeys: false});

chat.on('connect', (socket) => {
    socket.send('Welcome to /chat');
    socket.addEventListener('message', (msg) => socket.send(`Echo: ${msg.data}`));
});

Now reachable at ws://localhost:5173/chat.

4. Svelte client component

<script lang="ts">
    import WebSocket from '@sourceregistry/sveltekit-websockets';
</script>

<WebSocket action="?/ws">
    {#snippet message(data)}
        <p>{new Date().toLocaleTimeString()}: {data}</p>
    {/snippet}
</WebSocket>

| Prop | Type | Description | |--------------|-----------|-------------------------------------------------| | action | string | Form action that returns { url } | | url | string | Direct WebSocket URL, as an alternative to action | | data | T[] | Bindable, reactive array of received messages | | auto_open | boolean | Auto-connect on mount (default: true) | | controller | snippet | Render callback for manual open() / close() | | message | snippet | Render callback per received message |


Full API reference

For every configuration option, server event, low-level socket API (broadcast, send, raw(), graceful shutdown, rate limiting, timeouts, etc.), and the client-side Action/ActionSocket helpers, see API.md.

Contributing

See CONTRIBUTING.md.

License

Apache-2.0