Skip to content

Commit

Permalink
Fix heartbeat responses for realtime service
Browse files Browse the repository at this point in the history
  • Loading branch information
brettimus committed Dec 9, 2024
1 parent 39056ef commit 860e127
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
16 changes: 13 additions & 3 deletions api/src/lib/realtime/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/honc-telegram-bot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"drizzle-kit": "^0.28.1",
"wrangler": "^3.91.0"
}
}
}
6 changes: 4 additions & 2 deletions examples/honc-telegram-bot/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)`),
});
});
30 changes: 18 additions & 12 deletions examples/honc-telegram-bot/src/index.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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}`;

Expand All @@ -40,7 +39,7 @@ app.post('/webhook', async (c) => {
// });
// }

return c.text('OK', 200);
return c.text("OK", 200);
});

// CRUD for Messages
Expand All @@ -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);
});
Expand All @@ -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);
});
Expand All @@ -83,4 +89,4 @@ app.delete("/api/messages/:id", async (c) => {
return c.text(`Message with ID ${id} deleted.`);
});

export default instrument(app);
export default instrument(app);

0 comments on commit 860e127

Please sign in to comment.