{
const hono = new OpenAPIHono();
@@ -62,5 +66,5 @@ export function buildModule<
});
}
- return { routes, pluginId, hono, name, modules, cronJobs };
+ return { routes, pluginId, hono, name, modules, cronJobs, webSockets };
}
diff --git a/packages/vitnode/src/api/lib/plugin.ts b/packages/vitnode/src/api/lib/plugin.ts
index 1e4c5b2e2..8c27aaf76 100644
--- a/packages/vitnode/src/api/lib/plugin.ts
+++ b/packages/vitnode/src/api/lib/plugin.ts
@@ -2,6 +2,7 @@ import { OpenAPIHono } from "@hono/zod-openapi";
import type { CronJobConfig } from "./cron";
import type { BuildModuleReturn } from "./module";
+import type { WebSocketConfig } from "./websocket";
import { checkPluginId } from "./check-plugin-id";
@@ -9,6 +10,7 @@ export interface BuildPluginApiReturn {
cronJobs: Omit ({
@@ -23,17 +25,23 @@ export function buildApiPlugin ({
const hono = new OpenAPIHono();
const cronJobs: BuildPluginApiReturn["cronJobs"] = [];
+ const webSockets: BuildPluginApiReturn["webSockets"] = [];
modules.forEach(handler => {
hono.route(`/${handler.name}`, handler.hono);
handler.cronJobs?.forEach(cron => {
cronJobs.push({ ...cron, module: handler.name });
});
+
+ handler.webSockets?.forEach(webSocket => {
+ webSockets.push({ ...webSocket, module: handler.name });
+ });
});
return {
pluginId,
hono,
cronJobs,
+ webSockets,
};
}
diff --git a/packages/vitnode/src/api/lib/websocket.ts b/packages/vitnode/src/api/lib/websocket.ts
new file mode 100644
index 000000000..e60ed6b27
--- /dev/null
+++ b/packages/vitnode/src/api/lib/websocket.ts
@@ -0,0 +1,66 @@
+import type { Context } from "hono";
+import type { WSContext } from "hono/ws";
+
+import type { EnvVitNode } from "../middlewares/global.middleware";
+
+export { getWebSocketId } from "@/ws/types";
+
+/**
+ * WebSocket context for controlling the connection. Use it to `send`, `close`
+ * or read the `readyState`. The underlying runtime socket is exposed on
+ * `ws.raw` and is left generic so the same handler works across runtimes
+ * (node.js `ws`, Bun, ...).
+ */
+export type VitNodeWSContext = WSContext;
+
+/**
+ * Arguments passed to a registered WebSocket's `onMessage` handler.
+ *
+ * - `TReceive` - the payload the client sends for this socket.
+ * - `TSend` - the payload the server sends back to the client.
+ */
+export interface VitNodeWSMessageParams
+ Pushes a toast to the user in real time, on every browser where they
+ are signed in. Defaults to your own user id.
+ Send a notification
+