Gathering detailed insights and metrics for koa-imp-ws
Gathering detailed insights and metrics for koa-imp-ws
Gathering detailed insights and metrics for koa-imp-ws
Gathering detailed insights and metrics for koa-imp-ws
A maintained fork of `koa-easy-ws`, a simple Koa middleware for websocket handling
npm install koa-imp-ws
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
59 Commits
1 Branches
1 Contributors
Updated on Jan 13, 2024
Latest Version
1.1.1
Package Id
koa-imp-ws@1.1.1
Unpacked Size
25.29 kB
Size
6.76 kB
File Count
9
NPM Version
10.2.3
Node Version
20.10.0
Published on
Jan 13, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
8
Simple, easy to use, composable middleware for websocket handling in Koa
Note:
This is a fork of koa-easy-ws, but rewritten in TypeScript. The middleware has been simplified to always expose the server on ctx.wsServer
and the socket on ctx.ws()
.
This is NOT a drop-in replacement for koa-easy-ws
. Make sure to read is README well, since it has been adjusted for rewrite. The internals of this library are still the same and effectively functions in the same way, however some of the functions have been changed.
1const Koa = require("koa"); 2const websocket = require("koa-easy-ws"); 3 4const app = new Koa(); 5 6app.use(websocket()); 7app.use(async (ctx, next) => { 8 const socket = await ctx.ws(); // Retrieve socket 9 10 // Check if the connection upgrade was successful 11 if (socket) { 12 // now you have a ws instance, you can use it as you see fit 13 return ws.send("hello there"); 14 } 15 16 // we're back to regular old http here 17 ctx.body = "general kenobi"; 18});
Note: you will also need to install the ws
package (npm install --save ws
or yarn add ws
), it is linked only as a peer dependency.
First, you need to pass the koa-easy-ws middleware before the one handling your request. Remember to call it as a function, app.use(websocket())
, not app.use(websocket)
. This sets up on-demand websocket handling for the rest of the middleware chain.
The middleware adds the ctx.ws()
function whenever it detects an upgrade request, calling which handles the websocket and returns a ws instance. If not called, regular Koa flow continues, likely resulting in a client-side error.
You can easily compose koa-easy-ws with a routing library:
1const Koa = require("koa"); 2const Router = require("koa-router"); 3const websocket = require("koa-easy-ws"); 4 5const app = new Koa(); 6const router = new Router(); 7 8app.use(websocket()).use(router.routes()).use(router.allowedMethods()); 9 10// App websocket 11router.get("Obiwan", "/obiwan", async (ctx, next) => { 12 const socket = await ctx.ws(); 13 if (socket) { 14 socket.send("chancellor palpatine is evil"); 15 } 16}); 17 18router.get("Anakin", "/anakin", async (ctx, next) => { 19 const socket = await ctx.ws(); 20 if (socket) { 21 socket.send("the jedi are evil"); 22 socket.send("404"); 23 } 24}); 25 26// Route specific websocket 27router.get( 28 "Jar Jar is evil", 29 "/jar-jar", 30 authorize(), // Route specific middleware will take effect 31 websocket(), // Will override the `ctx.ws` and `ctx.wsServer` set at the toplevel middleware 32 async (ctx, next) => { 33 const socket = await ctx.ws(); 34 if (socket) { 35 socket.send("Me-sa was mastermind all-along"); 36 } 37 }, 38);
You can pass options to the underlying websocket server as part of the options object:
1app.use( 2 websocket({ 3 wsOptions: { 4 clientTracking: false, 5 maxPayload: 69420, 6 }, 7 }), 8);
The wsOptions
object will be forwarded to WebSocket.Server
unchanged, you can check its documentation for the available options.
If needed, you can use the websocket server exposed on ctx.wsServer
1const Koa = require("koa"); 2const websocket = require("koa-easy-ws"); 3 4const app = new Koa(); 5 6app.use(websocket()); 7 8app.use(async (ctx, next) => { 9 const socket = await ctx.ws(); 10 if (socket) { 11 console.log("found the server", ctx.wsServer); 12 } 13});
From here, the sky is the limit, unless you work for SpaceX.
Node's HTTP server doesn't send upgrade requests through the normal callback (and thus your Koa middleware chain) prior to version 10, preventing koa-easy-ws from handling them. Because of this, if you target Node 9 or earlier, you must pass your HTTP server to the middleware which handles the workaround:
1const server = http.createServer(app.callback()); 2 3app.use(websocket(server)); 4 5// alternatively, you can pass it as part of the options object: 6app.use(websocket({ server: server })); 7 8server.listen(process.env.PORT); // use this function instead of your app.listen() call
koa-easy-ws then automatically feeds any upgrade request into your regular middleware chain. If you wish to opt out and do this yourself, use the noServerWorkaround
option:
1app.use( 2 websocket({ 3 noServerWorkaround: true, 4 }), 5);
Pull requests are welcome. As always, be respectful towards each other. Currently the tests from the original repository have been removed. In future updates this library I will be fully testing it.
koa-imp-ws uses the MIT license, just like koa-easy-ws.
No vulnerabilities found.
No security vulnerabilities found.