diff --git a/src/brisa/request-context/index.ts b/src/brisa/request-context/index.ts index f4d92bfaf..7d728a3e0 100644 --- a/src/brisa/request-context/index.ts +++ b/src/brisa/request-context/index.ts @@ -1,4 +1,4 @@ -import { MatchedRoute } from "bun"; +import { MatchedRoute, ServerWebSocket } from "bun"; import { I18nFromRequest } from "../../types"; export default class RequestContext extends Request { @@ -6,9 +6,11 @@ export default class RequestContext extends Request { super(request); this.route = route; this.context = new Map(); + this.ws = globalThis.ws; } route?: MatchedRoute; context: Map; i18n?: I18nFromRequest; + ws?: ServerWebSocket; } diff --git a/src/cli/serve.tsx b/src/cli/serve.tsx index 10ae60576..57cea7e6e 100644 --- a/src/cli/serve.tsx +++ b/src/cli/serve.tsx @@ -10,6 +10,7 @@ import importFileIfExists from "../utils/import-file-if-exists"; import getConstants from "../constants"; import handleI18n from "../utils/handle-i18n"; import redirectTrailingSlash from "../utils/redirect-trailing-slash"; +import getImportableFilepath from "../utils/get-importable-filepath"; const { IS_PRODUCTION, @@ -22,6 +23,9 @@ const { ASSETS_DIR, } = getConstants(); +const WEBSOCKET_PATH = getImportableFilepath("websocket", ROOT_DIR); +const wsModule = WEBSOCKET_PATH ? await import(WEBSOCKET_PATH) : null + declare global { var ws: ServerWebSocket | undefined; } @@ -92,13 +96,18 @@ Bun.serve({ websocket: { open: (ws: ServerWebSocket) => { globalThis.ws = ws; + wsModule?.open?.(ws); }, - close: () => { + close: (...args) => { globalThis.ws = undefined; + wsModule?.close?.(...args); }, - message: () => { - /* void */ + message: (ws, message) => { + wsModule?.message?.(ws, message); }, + drain: (ws) => { + wsModule?.drain?.(ws); + } }, }); diff --git a/src/types/index.d.ts b/src/types/index.d.ts index bff75e805..7eff288d2 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -2,6 +2,7 @@ export interface RequestContext extends Request { context: Map; route?: MatchedRoute; i18n?: I18nFromRequest; + ws?: ServerWebSocket; } type Props = Record & { diff --git a/src/utils/load-layout/index.tsx b/src/utils/load-layout/index.tsx index 42223c49e..5f935e927 100644 --- a/src/utils/load-layout/index.tsx +++ b/src/utils/load-layout/index.tsx @@ -14,6 +14,8 @@ export default async function LoadLayout({ return ( + + Brisa {children}