vite-sveltekit-cf-ws Svelte Themes

Vite Sveltekit Cf Ws

A vite plugin. SvelteKit + Cloudflare + Websocket

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

Loading Svelte Themes