1.0.1 • Published 5 years ago

reed-koa-websocket v1.0.1

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

Reed-Koa-Websocket

Routable Websocket Wrapper for KOA (ES6+)

Sample

const Koa = require('koa'),
    Router = require('koa-router'),
    KoaWs = require('reed-koa-websocket')

const app = new Koa()
app.ws = new KoaWs(app)

const router = new Router()
router.all('/channel/:name', ctx => {

    ctx.websocket.send(`Hello ${ctx.params.name}`)

    ctx.websocket.on('message', msg => {
        // echo back
        ctx.websocket.send(JSON.stringify({
            params: { ...ctx.params },
            query: { ...ctx.query },
            msg
        }))
    })
})

app.ws.use(router.routes())
app.listen(8080)

// 
// let client = new WebSocket('ws://localhost:8080/channel/hans?foo=bar')
// ...

Class: KoaWs

constructor(app, wsOptions)

app: KOA Application

wsOptions: optional construction options for ws