This plugin just exposes httpServer without any dependencies. In theory, you can use any websocket framework you like.
Notice: for ws user, You may encounter the error RangeError: Invalid WebSocket frame: invalid status code xxxx
,
To avoid this error, specify a different port.
//vite.config.ts
export default defineConfig({
plugins: [sveltekit(),ws()],
server:{
hmr:{port:5678}
}
});
vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import ws from 'vite-sveltekit-node-ws';
export default defineConfig({
plugins: [sveltekit(), ws()]
});
hooks.server.ts
import {useServer} from "vite-sveltekit-node-ws";
import {WebSocketServer} from 'ws'
useServer((server) => {
const ss = new WebSocketServer({server})
ss.on('connection',a=>{
a.on('message',e=>{
a.send(`echo: ${e}`)
})
})
},(pathname:string)=>{
// It is optional
// You can block some requests to prevent them from being processed by SveltetKit
// return pathname.startsWith('/hello')
return false
})
import wsPlugin, {useServer} from "vite-sveltekit-node-ws";
serverHandle: ( server ) => void
pathHandle?: ( pathname: string ) => boolean