How d4c-chat works¶
d4c-chat is the chat running on the top page of d4c.dev. When a visitor writes, an AI secretary answers, and the owner reads the conversation in Telegram and can step in. This page gives the overall picture of how the three parties, the visitor's browser, the API, and the owner's Telegram, connect.
Three flows connecting visitor, secretary, and owner¶
- Message and reply: the API receives the visitor's message and streams the secretary's reply to the browser
- Notification: the visitor's messages and the secretary's replies are sent to the owner's Telegram
- Relay: when the owner replies to a notification, the secretary delivers it to the visitor's screen
Session: the unit of one visitor's conversation¶
The API keeps one visitor's conversation as a session. It holds the history of messages and replies, plus replies from the owner. It lives in the memory of the API process and disappears one hour after the last message. It is not stored anywhere.
The session id is passed to the browser in the response header of the reply. The browser holds on to the id and attaches it to the next message. If the server returns a different id because the API restarted or the session expired, the browser switches to that one. Because sessions live in memory, the API runs as a single instance.
Message and reply: sending with fetch, replies over SSE¶
The browser POSTs the message with fetch and receives the reply over SSE (Server-Sent Events, an HTTP mechanism where the server keeps streaming text one way to the browser), adding to the screen whatever has arrived.
Business information, development log, contact, and privacy policy are four fixed cards in the nav, and the browser shows the text it has prepared as-is. The secretary does not see the cards, so with every message the browser sends the cards already displayed, and the API adds only the cards not yet recorded to the conversation history. If the visitor asks about the content of a card they just read, the secretary can answer.
Notification: sending both sides' messages to the owner's Telegram¶
Both the visitor's messages and the secretary's replies are sent to the owner's Telegram. Each notification contains the session id, which serves as the address for replies.
Relay: the secretary delivers the owner's reply to the visitor¶
The owner answers a notification with Telegram's "Reply". The API receives it through a webhook (a mechanism where Telegram sends the replies it receives to the API's URL) and holds the reply on the session with that id. For anything that is not a reply to a notification, or a reply to an expired session, the API returns a warning to Telegram.
A held reply waits 15 seconds. If the visitor writes a next message during that time, the secretary weaves the content of the reply into its answer. If not, the secretary generates a relay message from the reply and pushes it to the visitor's screen.
The push arrives over an SSE connection separate from the message exchange. The browser uses EventSource (the browser's API for receiving SSE) to open a connection specifying the session id and keeps it open, reconnecting each time it receives one relay or 240 seconds pass. A relay generated while there is no connection is held on the session by the API and sent first on the next connection.
Relays are distributed in the following three ways.
- The unit of delivery is the connection: a queue is kept per SSE connection, and relays go to every connection open on the same session. If a single queue were shared, a connection that was waiting earlier (including one whose browser has already left) would take the relay
- Connection liveness checks: a comment line (
: ping) is sent on each SSE connection every 15 seconds. This detects browsers that left silently and also keeps proxies along the way from dropping the connection - Reconnect when the tab returns: after 5 consecutive connection failures, reconnection stops, but if the connection is closed when the tab comes back to the foreground, the failure count is reset and it reconnects. Relays that arrive while the visitor has switched to another app on a phone can still be received on return
Created 2026-11-05 · Updated 2026-11-07 · Machine-translated from the Japanese original.