Skip to content

Commit aad5402

Browse files
committed
fix(backend): send nodeId in ws message on connection open
Oak doesn't set headers correctly when upgrading to the ws protocol.
1 parent a2085eb commit aad5402

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

backend/src/middleware/cluster.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { Middleware } from '@oak/oak';
22

3-
const nodeId = crypto.randomUUID();
3+
import { fromEnv } from '../utils/fromEnv.ts';
4+
5+
export const nodeId = crypto.randomUUID();
6+
7+
export const inCluster = fromEnv('CLUSTER', {
8+
defaultValue: false,
9+
allowDefaultValueInProd: true,
10+
type: Boolean,
11+
});
412

513
export default function (): Middleware {
614
return async (ctx, next) => {

backend/src/routes/v1/canvas/gateway.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from '../../../db/canvas.ts';
1010
import { auth } from '../../../middleware/index.ts';
1111
import { httpError } from '../../../utils/httpError.ts';
12+
import { inCluster, nodeId } from '../../../middleware/cluster.ts';
1213

1314
const connections = new Set<WebSocket>();
1415

@@ -36,6 +37,14 @@ function gateway(ctx: Context) {
3637

3738
ws.onopen = () => {
3839
connections.add(ws);
40+
41+
if (inCluster)
42+
ws.send(
43+
JSON.stringify({
44+
type: 'cluster',
45+
servedBy: nodeId,
46+
})
47+
);
3948
};
4049

4150
ws.onmessage = async (event) => {

0 commit comments

Comments
 (0)