项目
在 GitHub 查看
tinyhttp/tinyws
关于
🚡 适用于 Node.js 的轻量 WebSocket 中间件。
网站与应用开发可以做什么
- Small size (498B)
- Easy to use (only req.ws and nothing else)
- Framework-agnostic (works with tinyhttp, express etc)
- Written in TypeScript
实测星标增长
- 24小时
- 0
- 7天
- 0
- 15天
- 0
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README.md
原文tinyws is a WebSocket middleware for Node.js based on ws, inspired by koa-easy-ws.
Check the chat example out to get familiar with tinyws.
Features
- Small size (498B)
- Easy to use (only
req.wsand nothing else) - Framework-agnostic (works with tinyhttp, express etc)
- Written in TypeScript
- Pure ESM
Why not express-ws?
because express-ws is...
- Abandoned since 2018 💀
- Doesn't come with types out of the box (have to install
@types/express-ws) - Not compatible with tinyhttp and polka
- Buggy as hell
- Doesn't have tests
Install
pnpm i ws tinyws
Example
import { App, Request } from '@tinyhttp/app'
import { tinyws, TinyWSRequest } from 'tinyws'
const app = new App<Request & TinyWSRequest>()
app.use('/ws', async (req, res) => {
if (req.ws) {
const ws = await req.ws()
return ws.send('hello there')
} else {
res.send('Hello from HTTP!')
}
})
const server = app.listen(3000)
tinyws(app, server)
Restricting WebSocket to specific paths
You can restrict WebSocket handling to specific paths using the paths option:
// Single path
tinyws(app, server, { paths: '/ws' })
// Multiple paths
tinyws(app, server, { paths: ['/ws', '/socket'] })
See examples for express and polka integration.
探索相关项目
加载中…