Skip to content

vinsonchuong/passing-notes-websocket

Repository files navigation

passing-notes-websocket

npm CI Status

A middleware for accepting WebSocket connections

Usage

Install passing-notes-websocket by running:

yarn add passing-notes-websocket

Then, compose it with other middleware, or at least a default handler:

import {compose} from 'passing-notes'
import serveWebSocket from 'passing-notes-websocket'

export default compose(
  serveWebSocket((ws) => {
    ws.on('message', (message) => {
      console.log(message)
    })

    ws.send('Hello World!')
  }),
  () => () => ({status: 404})
)

serveWebSocket will look for HTTP/1.1 Upgrade requests that ask for Upgrade: websocket. It will then negotiate the upgrade and provide a WebSocket object. Otherwise, it delegates to the next middleware.

About

A middleware for accepting WebSocket connections

Resources

License

Stars

Watchers

Forks

Packages

No packages published