vite-sveltekit-cf-ws

Supports local development and cloudflare.

demo

Usage

vite.config.ts

import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import ws from 'vite-sveltekit-cf-ws';

export default defineConfig({
    plugins: [sveltekit(), ws()]
});

hooks.server.ts

import {handleUpgrade} from "vite-sveltekit-cf-ws";

handleUpgrade((req, createWebsocketServer) => {
    const pathname = new URL(req.url || '', 'ws://base.url').pathname
    if (pathname === '/hello') {
        const server = createWebsocketServer()
        server.accept()
        server.addEventListener('message', ({data}) => {
            server.send(`[ws server] received message: "${data}"`)
        })
    }
})

Top categories

svelte logo

Need a Svelte website built?

Hire a professional Svelte developer today.
Loading Svelte Themes