🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

vite-sveltekit-node-ws

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-sveltekit-node-ws

This plugin just exposes httpServer without any dependencies. In theory, you can use any websocket framework you like.

latest
Source
npmnpm
Version
0.0.8
Version published
Maintainers
1
Created
Source

vite-sveltekit-node-ws

This plugin just exposes httpServer without any dependencies. In theory, you can use any websocket framework you like.

Try it with socket.io

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
        }
    }
});

Support

  • dev
  • preview
  • online

Usage

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
})

API

import wsPlugin, {useServer} from "vite-sveltekit-node-ws";

useServer(serverHandle, pathHandle?)

serverHandle: ( server ) => void
pathHandle?:  ( pathname: string ) => boolean 
  • server : the httpServer from Polka(production mode) or from Vite(development.preview mode)
  • pathHandle: return true if the request no need to be handled by sveltekit
  • pathname: the request's pathname

Keywords

sveltekit

FAQs

Package last updated on 18 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts