Skip to content

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

Notifications You must be signed in to change notification settings

appuri/koa-websocket

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-websocket

Circle CI

Installation

npm install koa-websocket

Usage

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

const app = websockify(koa());

// Note it's app.ws.use and not app.use
app.ws.use(route.all('/test/:id', function* (next) {
  // `this` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object.
  // the websocket is added to the context on `this.websocket`.
  this.websocket.send('Hello World');
  this.websocket.on('message', function(message) {
    // do something with the message from client
    console.log(message);
  });
  // yielding `next` will pass the context (this) on to the next ws middleware
  yield next;
}));

app.listen(3000);

About

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

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%