koa-wss
💙 Support wss://
in your Koa app 💙
This is a fork/copy of the excellent package koa-websocket by Jonathan Cremin, with secure Web Socket functionality added.
Koa's listen
method just calls http.createServer(options).listen(...)
, so this calls https.createServer(options).listen(...)
instead and provides a parameter to pass in the HTTPS options (like the certificate and stuff).
If you don't supply an httpsOptions
argument, koa-wss will do what koa-websocket does and just use Koa's built-in listen
method.
See Koa's docs about this here.
Installation
npm install koa-wss --save
Usage
Example with Let's Encrypt (the Greenlock package):
const Koa = ;const greenlock = ;const websockify = ; const le = greenlock; // the magic happens right hereconst app = ; // async/await is of course supportedappws; app;
Another example:
const fs = ;const path = ;const Koa = ;const route = ;const websockify = ; // using a local certificate, but whatever you normally put in HTTPS options works hereconst httpsOptions = key: fs cert: fs; // the main eventconst app = ; // Note it's app.ws.use and not app.use// This example uses koa-routeappws; app;
API
websockify(KoaApp, WebSocketOptions, httpsOptions)
The WebSocket options object just get passed right through to the new WebSocketServer
call.
koa-wss passes in { server: httpsServer }
automatically because that's the whole point.
The HTTPS options object gets passed right into https.createServer(options)
. If you don't specify
these options with your certificate info, it will just set up an HTTP Koa server (the default).
License
MIT