• Stars
    star
    406
  • Rank 106,421 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 3 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🚑 tiny WebSocket middleware for Node.js
tinyws

🚑 tiny WebSocket middleware for Node.js


Version Downloads GitHub Workflow Status Codecov

I'm building Flash - a service to deploy websites and apps on the new decentralized stack.

If you'd like to try or collab, dm or email

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.ws and 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<any, Request & TinyWSRequest>()

app.use(tinyws())

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

app.listen(3000)

See examples for express and polka integration.

More Repositories