Supports local development and cloudflare.
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}"`)
})
}
})