Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kudos/koa-websocket
Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
https://github.com/kudos/koa-websocket
javascript koa nodejs websockets ws
Last synced: about 1 month ago
JSON representation
Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
- Host: GitHub
- URL: https://github.com/kudos/koa-websocket
- Owner: kudos
- License: mit
- Created: 2015-03-08T23:03:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-02-27T07:56:31.000Z (over 1 year ago)
- Last Synced: 2024-04-08T09:03:07.718Z (7 months ago)
- Topics: javascript, koa, nodejs, websockets, ws
- Language: JavaScript
- Size: 826 KB
- Stars: 257
- Watchers: 4
- Forks: 36
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# koa-websocket
[![CI Status](https://github.com/kudos/koa-websocket/actions/workflows/node.js.yml/badge.svg)](https://github.com/kudos/koa-websocket/actions)
> Koa v2 is now the default. For Koa v1 support install with koa-websocket@2 and see the `legacy` branch.
Supports `ws://` and `wss://`
## Installation
`npm install koa-websocket`
## Usage
See examples directory for a simple implementation.
### Let's Encrypt
Example with Let's Encrypt ([the Greenlock package](https://git.daplie.com/Daplie/greenlock-koa)):
```js
const Koa = require('koa');
const greenlock = require('greenlock-express');
const websockify = require('koa-websocket');
const le = greenlock.create({
// all your sweet Let's Encrypt options here
});
// the magic happens right here
const app = websockify(new Koa(), wsOptions, le.httpsOptions);
app.ws.use((ctx) => {
// the websocket is added to the context as `ctx.websocket`.
ctx.websocket.on('message', function(message) {
// do something
});
});
app.listen(3000);
```## 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