01
SSE loop#
A production implementation should reconnect with backoff and de-duplicate event IDs. Uses the EventSource global available in Node.js 22+, Deno, and browsers.
TypeScript
const BASE = "https://botinbox.dev";
const KEY = process.env.BOTINBOX_API_KEY!;
const events = new EventSource(`${BASE}/v0/events?token=${KEY}`);
events.addEventListener("message.received", async (event) => {
const { data } = JSON.parse(event.data);
await fetch(`${BASE}/v0/inboxes/${data.inbox_id}/messages`, {
method: "POST",
headers: { Authorization: `Bearer ${KEY}`, "Content-Type": "application/json" },
body: JSON.stringify({
to: [data.message.fromAddress],
subject: `Re: ${data.message.subject}`,
text: "Thanks, I received your message.",
reply_to_message_id: data.message.id,
}),
});
});