From 308df5a5e6d268d7de4a99e570fc9022b48c3cf6 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 21 Jan 2025 18:13:31 -0500 Subject: [PATCH] feat: add persistent connections to ws --- index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/index.js b/index.js index 608366a..09a137e 100644 --- a/index.js +++ b/index.js @@ -26,6 +26,7 @@ let eta = new Eta({ views: viewpath, cache: false, autoEscape: true }); let routes = { GET: [], POST: [], PUT: [], DELETE: [] }; const wsRoutes = {} +const wsConnections = {} app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); @@ -121,6 +122,7 @@ function bootstrapWebsocketHandler(route) { let handler = vm.runInContext(`${route.handler}\n\nhandler;`, context); if (!wsRoutes[routeWithPrefix(route)]) { + wsConnections[routeWithPrefix(route)] = new Set(); // initialize ws router. future updates will only require updating // the wsRoutes dict, not create a whole new route wsRouter.ws(routeWithPrefix(route), (ws, req) => { @@ -144,6 +146,8 @@ function bootstrapWebsocketHandler(route) { ); }; + ws.clients = wsConnections[routeWithPrefix(route)]; + return wsRoutes[routeWithPrefix(route)](ws, req) }); }