feat: implement WebSocket for real-time trial updates

- Create standalone WebSocket server (ws-server.ts) on port 3001 using Bun
- Add ws_connections table to track active connections in database
- Create global WebSocket manager that persists across component unmounts
- Fix useWebSocket hook to prevent infinite re-renders and use refs
- Fix TrialForm Select components with proper default values
- Add trialId to WebSocket URL for server-side tracking
- Update package.json with dev:ws script for separate WS server
This commit is contained in:
2026-03-22 00:48:43 -04:00
parent 20d6d3de1a
commit a5762ec935
9 changed files with 1257 additions and 481 deletions
+19
View File
@@ -485,6 +485,25 @@ export const trials = createTable("trial", {
metadata: jsonb("metadata").default({}),
});
export const wsConnections = createTable("ws_connection", {
id: uuid("id").notNull().primaryKey().defaultRandom(),
trialId: uuid("trial_id")
.notNull()
.references(() => trials.id, { onDelete: "cascade" }),
clientId: text("client_id").notNull().unique(),
userId: text("user_id"),
connectedAt: timestamp("connected_at", { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
});
export const wsConnectionsRelations = relations(wsConnections, ({ one }) => ({
trial: one(trials, {
fields: [wsConnections.trialId],
references: [trials.id],
}),
}));
export const steps = createTable(
"step",
{