Need help with koa-websocket?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

kudos
228 Stars 34 Forks MIT License 94 Commits 19 Opened issues

Description

Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.

Services available

!
?

Need anything else?

Contributors list

# 78,714
plex
electro...
Docker
Less
33 commits
# 53,227
Ruby
SQL
postgre...
Bootstr...
5 commits
# 296,809
CSS
Shell
tms
Node.js
1 commit
# 27,504
JavaScr...
HTML
Node.js
Koa
1 commit
# 332,818
Emacs
PHP
keyboar...
emacs-l...
1 commit
# 10,420
Crystal
cpp11
repl
Electro...
1 commit
# 2,331
preact
JavaScr...
repl
Redux
1 commit

koa-websocket

Circle CI

Koa v2 is now the default. For Koa v1 support install with [email protected] and see the

legacy
branch.

Supports

ws://
and
wss://

Installation

npm install koa-websocket

Usage

const Koa = require('koa'),
  route = require('koa-route'),
  websockify = require('koa-websocket');

const app = websockify(new Koa());

// Regular middleware // Note it's app.ws.use and not app.use app.ws.use(function(ctx, next) { // return next to pass the context (ctx) on to the next ws middleware return next(ctx); });

// Using routes app.ws.use(route.all('/test/:id', function (ctx) { // ctx is the regular koa context created from the ws onConnection socket.upgradeReq object. // the websocket is added to the context on ctx.websocket. ctx.websocket.send('Hello World'); ctx.websocket.on('message', function(message) { // do something with the message from client console.log(message); }); }));

app.listen(3000);

Example with Let's Encrypt (the Greenlock package):

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);

With custom websocket options.

const Koa = require('koa'),
  route = require('koa-route'),
  websockify = require('koa-websocket');

const wsOptions = {}; const app = websockify(new Koa(), wsOptions);

app.ws.use(route.all('/', (ctx) => { // the websocket is added to the context as ctx.websocket. ctx.websocket.on('message', function(message) { // print message from the client console.log(message); }); }));

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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.