diff --git a/packages/server/src/api/controllers/table/index.ts b/packages/server/src/api/controllers/table/index.ts index 1ad8542afd..af3c2c0c15 100644 --- a/packages/server/src/api/controllers/table/index.ts +++ b/packages/server/src/api/controllers/table/index.ts @@ -168,7 +168,7 @@ export async function migrate(ctx: UserCtx) { let result = await sdk.tables.migrate(table, oldColumn, newColumn) for (let table of result.tablesUpdated) { - builderSocket?.emitTableUpdate(ctx, table) + builderSocket?.emitTableUpdate(ctx, table, { includeSelf: true }) } ctx.status = 200 diff --git a/packages/server/src/websockets/builder.ts b/packages/server/src/websockets/builder.ts index d2fdbca20c..b1ef473d6c 100644 --- a/packages/server/src/websockets/builder.ts +++ b/packages/server/src/websockets/builder.ts @@ -1,5 +1,5 @@ import authorized from "../middleware/authorized" -import { BaseSocket } from "./websocket" +import { BaseSocket, EmitOptions } from "./websocket" import { permissions, events, context } from "@budibase/backend-core" import http from "http" import Koa from "koa" @@ -100,11 +100,17 @@ export default class BuilderSocket extends BaseSocket { }) } - emitTableUpdate(ctx: any, table: Table) { - this.emitToRoom(ctx, ctx.appId, BuilderSocketEvent.TableChange, { - id: table._id, - table, - }) + emitTableUpdate(ctx: any, table: Table, options?: EmitOptions) { + this.emitToRoom( + ctx, + ctx.appId, + BuilderSocketEvent.TableChange, + { + id: table._id, + table, + }, + options + ) gridSocket?.emitTableUpdate(ctx, table) } diff --git a/packages/server/src/websockets/websocket.ts b/packages/server/src/websockets/websocket.ts index ffaf9e2763..a0dc388d4f 100644 --- a/packages/server/src/websockets/websocket.ts +++ b/packages/server/src/websockets/websocket.ts @@ -11,6 +11,12 @@ import { SocketSession } from "@budibase/types" import { v4 as uuid } from "uuid" import { createContext, runMiddlewares } from "./middleware" +export interface EmitOptions { + // Whether to include the originator of the request from the broadcast, + // defaults to false. + includeSelf?: boolean +} + const anonUser = () => ({ _id: uuid(), email: "user@mail.com", @@ -270,10 +276,17 @@ export class BaseSocket { // Emit an event to everyone in a room, including metadata of whom // the originator of the request was - emitToRoom(ctx: any, room: string | string[], event: string, payload: any) { - this.io.in(room).emit(event, { - ...payload, - apiSessionId: ctx.headers?.[Header.SESSION_ID], - }) + emitToRoom( + ctx: any, + room: string | string[], + event: string, + payload: any, + options?: EmitOptions + ) { + let emitPayload = { ...payload } + if (!options?.includeSelf) { + emitPayload.apiSessionId = ctx.headers?.[Header.SESSION_ID] + } + this.io.in(room).emit(event, emitPayload) } }