Registry / devops / svelte-websocket

svelte-websocket

JSON →
library1.0.0jsnpmunverified

Bootstrapping helpers for attaching WebSocket servers (e.g., Socket.IO, ws) to a SvelteKit application in both development (via Vite plugin) and production (via Node runtime). Current stable version 1.0.0, requires Node >=18 and Vite ^7 || ^8. Unlike manual setups, it provides a unified API to expose the same WebSocket function across environments, handles lifecycle deduplication, and keeps runtime imports Vite-free.

npm install svelte-websocket
INSTALL
IMPORT
SIG · SVELTE-WEBSOCKET
S
svelte-websocket
devopsjavascriptv1.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

svelteKitWebSocketPlugin
import { svelteKitWebSocketPlugin } from 'svelte-websocket/vite'
import { svelteKitWebSocketPlugin } from 'svelte-websocket'
Plugin is only available from the '/vite' subpath; root entry is runtime-safe
exposeWebSocketServer
import { exposeWebSocketServer } from 'svelte-websocket'
import { exposeWebSocketServer } from 'svelte-websocket/vite'
Runtime helpers are in the root entry; importing from '/vite' would pull Vite into production
startWebSocketServer
import { startWebSocketServer } from 'svelte-websocket'
const startWebSocketServer = require('svelte-websocket')
ESM-only package; no CommonJS support
getExposedWebSocketServer
import { getExposedWebSocketServer } from 'svelte-websocket'
Typically used in your custom Node server to retrieve the bootstrap function from globalThis

Demonstrates full setup: define a WebSocket server function, attach it via Vite plugin for dev, expose it for production via hooks.server.ts, and start it from a custom adapter-node server.

// src/lib/server/websocket/index.ts import { Server } from 'socket.io'; import type { Server as HttpServer } from 'node:http'; export function webSocketServer(httpServer: HttpServer) { const io = new Server(httpServer, { cors: { origin: '*' } }); io.on('connection', (socket) => { socket.emit('welcome', 'Realtime server connected'); }); } // vite.config.ts import { defineConfig } from 'vite'; import { sveltekit } from '@sveltejs/kit/vite'; import { svelteKitWebSocketPlugin } from 'svelte-websocket/vite'; export default defineConfig({ plugins: [sveltekit(), svelteKitWebSocketPlugin()] }); // src/hooks.server.ts import { exposeWebSocketServer } from 'svelte-websocket'; import { webSocketServer } from '$lib/server/websocket'; exposeWebSocketServer(webSocketServer); // Custom production server (adapter-node) import express from 'express'; import { createServer } from 'node:http'; import { handler } from './build/handler.js'; const app = express(); const server = createServer(app); const startRealtimeServer = globalThis.startRealtimeServer; if (startRealtimeServer) { await startRealtimeServer(server); } app.use(handler); server.listen(3000);
Debug
Known issues
gotchaThe Vite plugin default module path is 'src/lib/server/websocket/index.ts' and export name 'webSocketServer'. If your module/function is elsewhere, you must configure options.
fix
Pass { modulePath: 'your-path', exportName: 'yourExport' } to svelteKitWebSocketPlugin.
affects: >=1.0.0
deprecatedNo deprecated features yet; package is at v1.0.0.
fix
N/A
affects: <1.0.0
gotchaImporting runtime helpers from 'svelte-websocket' is safe; importing from 'svelte-websocket/vite' in production will unnecessarily bundle Vite.
fix
Always use the root entry for runtime helpers and '/vite' for the plugin.
affects: >=1.0.0
breakingPackage is ESM-only, no CommonJS support. Using require() will throw ERR_REQUIRE_ESM.
fix
Use dynamic import() or switch to an ESM project.
affects: >=1.0.0
Errors
Common errors & fixes
ERR_REQUIRE_ESM
Using require() on an ESM-only package
fix
Use import('svelte-websocket') or set "type": "module" in package.json and use import.
Cannot find module 'svelte-websocket/vite'
Missing 'vite' peer dependency or incorrect entry path
fix
Install vite: npm install vite@^7.0.0. Ensure you are importing from 'svelte-websocket/vite' not from a different path.
The requested module 'svelte-websocket' does not provide an export named 'svelteKitWebSocketPlugin'
Importing the plugin from the root entry instead of '/vite'
fix
Change import to: import { svelteKitWebSocketPlugin } from 'svelte-websocket/vite';
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies
viterequiredPeer dependency required for the Vite plugin entrypoint (svelte-websocket/vite)
Agent activity
6 hits · last 30 days
node
4
Amazon
1
Resources
svelte-websocket — npm install svelte-websocket · libregistry