koa-websocket
forked fromtomjs-koa-websocket
Koa v2 is now the default. For Koa v1 support install with koa-websocket@2 and see the
legacy
branch.
Supports ws://
and wss://
add ctx.websocket_server
Installation
npm install koa-websocket
Usage
const Koa = route = websockify = ; const app = ; // Regular middleware// Note it's app.ws.use and not app.useappws; // Using routesappws; app;
Example with Let's Encrypt (the Greenlock package):
const Koa = ;const greenlock = ;const websockify = ; const le = greenlock; // the magic happens right hereconst app = ; appws; app;
With custom websocket options.
const Koa = route = websockify = ; const wsOptions = {};const app = ; appws; app;
API
websockify(KoaApp, [WebSocketOptions], [httpsOptions])
The WebSocket options object just get passed right through to the new WebSocketServer(options)
call.
The optional HTTPS options object gets passed right into https.createServer(options)
. If the HTTPS options are
passed in, koa-websocket will use the built-in Node HTTPS server to provide support for the wss://
protocol.
License
MIT