From 860e12706b382932e574e17f71a1676c5185710e Mon Sep 17 00:00:00 2001 From: Brett Beutell Date: Mon, 9 Dec 2024 18:47:53 +0100 Subject: [PATCH] Fix heartbeat responses for realtime service --- api/src/lib/realtime/index.ts | 16 ++++++++--- examples/honc-telegram-bot/package.json | 2 +- examples/honc-telegram-bot/src/db/schema.ts | 6 +++-- examples/honc-telegram-bot/src/index.ts | 30 ++++++++++++--------- 4 files changed, 36 insertions(+), 18 deletions(-) diff --git a/api/src/lib/realtime/index.ts b/api/src/lib/realtime/index.ts index ba45a1b65..de4dfa7f1 100644 --- a/api/src/lib/realtime/index.ts +++ b/api/src/lib/realtime/index.ts @@ -21,9 +21,19 @@ export function setupRealtimeService({ ); wsConnections.add(ws); - ws.on("ping", () => { - logger.debug("ping"); - ws.send("pong"); + // NOTE - The `react-use-websocket` library's ping message was not getting picked up as an actual ping frame here, + // so below, we use `ws.on("message", (data) => { ... })` to detect the ping message and send a pong back instead. + // ws.on("ping", () => { + // logger.debug("ping"); + // // ws.send("pong"); + // ws.pong(); + // }); + ws.on("message", (data) => { + const message = data.toString(); + if (message === "ping") { + logger.debug("Received ping from websocket client"); + ws.send("pong"); + } }); ws.on("error", (err) => { diff --git a/examples/honc-telegram-bot/package.json b/examples/honc-telegram-bot/package.json index 97d7a7752..56bbaeb84 100644 --- a/examples/honc-telegram-bot/package.json +++ b/examples/honc-telegram-bot/package.json @@ -21,4 +21,4 @@ "drizzle-kit": "^0.28.1", "wrangler": "^3.91.0" } -} \ No newline at end of file +} diff --git a/examples/honc-telegram-bot/src/db/schema.ts b/examples/honc-telegram-bot/src/db/schema.ts index 45814153f..6233afdbe 100644 --- a/examples/honc-telegram-bot/src/db/schema.ts +++ b/examples/honc-telegram-bot/src/db/schema.ts @@ -14,7 +14,9 @@ export const users = sqliteTable("users", { export const messages = sqliteTable("messages", { id: integer("id", { mode: "number" }).primaryKey(), - userId: integer("user_id").notNull().references(() => users.id), + userId: integer("user_id") + .notNull() + .references(() => users.id), content: text("content").notNull(), createdAt: text("created_at").notNull().default(sql`(CURRENT_TIMESTAMP)`), -}); \ No newline at end of file +}); diff --git a/examples/honc-telegram-bot/src/index.ts b/examples/honc-telegram-bot/src/index.ts index ed1064c08..8ed55c906 100644 --- a/examples/honc-telegram-bot/src/index.ts +++ b/examples/honc-telegram-bot/src/index.ts @@ -1,7 +1,7 @@ import { instrument } from "@fiberplane/hono-otel"; +import { eq } from "drizzle-orm"; import { drizzle } from "drizzle-orm/d1"; import { Hono } from "hono"; -import { eq } from "drizzle-orm"; import * as schema from "./db/schema"; type Bindings = { @@ -15,8 +15,7 @@ app.get("/", (c) => { return c.text("Welcome to the Telegram Bot API!"); }); - -app.post('/webhook', async (c) => { +app.post("/webhook", async (c) => { const TOKEN = c.env.TELEGRAM_API_TOKEN; const TELEGRAM_API = `https://api.telegram.org/bot${TOKEN}`; @@ -40,7 +39,7 @@ app.post('/webhook', async (c) => { // }); // } - return c.text('OK', 200); + return c.text("OK", 200); }); // CRUD for Messages @@ -54,10 +53,13 @@ app.post("/api/messages", async (c) => { const db = drizzle(c.env.DB); const { userId, content } = await c.req.json(); - const [newMessage] = await db.insert(schema.messages).values({ - userId: userId, - content: content, - }).returning(); + const [newMessage] = await db + .insert(schema.messages) + .values({ + userId: userId, + content: content, + }) + .returning(); return c.json(newMessage); }); @@ -67,9 +69,13 @@ app.put("/api/messages/:id", async (c) => { const id = Number.parseInt(c.req.param("id")); const { content } = await c.req.json(); - const [updatedMessage] = await db.update(schema.messages).set({ - content: content, - }).where(eq(schema.messages.id, id)).returning(); + const [updatedMessage] = await db + .update(schema.messages) + .set({ + content: content, + }) + .where(eq(schema.messages.id, id)) + .returning(); return c.json(updatedMessage); }); @@ -83,4 +89,4 @@ app.delete("/api/messages/:id", async (c) => { return c.text(`Message with ID ${id} deleted.`); }); -export default instrument(app); \ No newline at end of file +export default instrument(app);