Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
koa-websocket
Advanced tools
Readme
Koa v2 is now the default. For Koa v1 support install with koa-websocket@2 and see the
legacy
branch.
Supports ws://
and wss://
npm install koa-websocket
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);
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.
MIT
FAQs
Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
The npm package koa-websocket receives a total of 18,528 weekly downloads. As such, koa-websocket popularity was classified as popular.
We found that koa-websocket demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.