Read receipts, file sharing and emoji reactions in a website chat: three small features, three design decisions
We recently added three features to our website live chat widget that every messaging app has had for years: sending files both ways, read receipts, and emoji reactions on messages. None of them is hard on its own. Each one still forced a decision we didn't expect, so here are the three decisions and why we made them.
1. A read receipt is a number, not a flag on every message
The obvious way to store "read" is a boolean on each message. It works until you look at what actually happens in a chat: an agent opens a conversation with 14 unread visitor messages, and all 14 become read at once. With a per-message flag that's 14 writes and 14 events to every open tab.
Messages in a conversation already have a sequence number, so we store one number per side instead:
conversation.agent_read_seq = last message the agents have seen conversation.visitor_read_seq = last message the visitor has seen A message is read when its sequence is less than or equal to the other side's number. Opening a conversation is one write and one small event (read, with the new sequence), and the widget redraws every tick at or below it.
The one rule that matters: the number only goes up. Two agents in two tabs, a phone that was offline for ten minutes and then reconnects, a retried request: all of these can try to write an older value after a newer one. So the update is conditional:
UPDATE conversations SET read_seq = :seq WHERE id = :id AND read_seq < :seq If nothing changed, nothing is broadcast either. We do the same "keep the larger one" merge when a full conversation record is saved, so a save that started before the read event can't roll it back.
2. Trust the bytes, not the file name
Visitors can now send photos, PDFs, Office documents, text files and ZIPs up to 10 MB. A file name is just a string the browser sends, so a file called invoice.pdf can be anything.
Every upload is checked against what its first bytes actually look like:
- Pictures are taken by their bytes alone, whatever they're called. A PNG named
photo.jpgis stored and served as a PNG. - A
.pdfmust start like a PDF,.docx/.xlsx/.pptxmust be a ZIP container, old.doc/.xls/.pptmust have the OLE header, and.txt/.csvmust decode as text. - Anything else is refused with one plain message: send a picture, PDF, Office document, text file or ZIP of up to 10 MB.
Files are served with the type we decided on, never the one the client sent, and with nosniff. Pictures show inline; every other file is sent as a download (as a generic binary type, except PDFs). The file name is cleaned too: path parts, control characters and quotes are removed and it's cut to 120 characters, keeping the extension.
3. Some apps can't take your upload, so give them a link that expires
The same inbox also answers messages from chat apps such as LINE. When an agent attaches a file to a reply, the visitor should get it as a real picture or file in that app, not as a link.
Some apps take the upload directly. Others don't: they want a URL they can fetch. So for those we create a signed link to the file:
/w/f/{token}/{file name} token = base64(workspace, conversation, file, expiry) + "." + HMAC(that, workspace secret) The link works for 30 days, needs no session, and can't be changed to point at another file. Where an app only takes pictures, a document goes as the link with its name.
Emoji reactions
Reactions were the easy one, because a reaction is just an update to a message: the message's reaction list changes and every open view gets a message_updated event with the new list. Each person can add or remove each emoji once per message.
What we left out
Read receipts and reactions stay inside the website chat for now: they aren't sent on to LINE or other chat apps, where each platform has its own rules about read state. If you've built that bridge, we'd like to hear how it went.
We build Hibira Live Chat, where these features are live on every plan, including the free one.