July 15, 2026
Designing an Embeddable Chat Widget
How I approach a widget that runs on websites I don't control: isolation, a small public API, and failing quietly.
- System Design
- Security
- WebSocket
- Go
I built a website chat widget: businesses paste a snippet on their site, visitors chat, and the business's team answers from the same inbox they use for other channels.
It looks like a frontend task. Most of the work was deciding where the trust boundary sits.
The constraint
The widget runs on pages I don't control: any CSS, any scripts, any security policy. Two rules had to hold:
- The host page must not be able to break the widget.
- The widget must not be able to break, or slow down, the host page.
The shape
The snippet loads a tiny loader script. The loader adds one iframe, and everything else lives inside it: the bubble, the chat UI and all network calls.
Host page ── loader ── iframe (widget UI)
│ HTTPS + WebSocket
Public widget API
│
Existing messaging backend → team inbox
If the service is down, or the domain isn't allowed, the page carries on as if the widget wasn't there.
iframe or Shadow DOM
Both are common. Shadow DOM is lighter. An iframe gives stronger isolation:
- It is its own document, so host styles can't reach it.
- Host scripts can't touch the widget's code.
- Fewer "the widget looks broken on my site" cases.
Because the snippet is the same either way, the choice stays reversible without asking anyone to change their site. Public chat products such as Intercom, Crisp and Chatwoot use the same loader-plus-iframe pattern.
A small public surface
The widget API is public by design, so I kept it small and separate from the staff-facing API:
- Its own middleware for CORS, rate limits and request size.
- A domain allowlist per widget, checked before a session starts.
- Short-lived, scoped visitor tokens instead of accounts.
- A WebSocket for real-time replies.
Past that point the widget reuses the existing messaging pipeline, so it became one more channel rather than a separate system.
Untrusted text, everywhere
Visitor messages are untrusted input that ends up on a staff screen. My rule: store text exactly as sent, and encode it only where it is shown. I keep a list of malicious test strings so the rule stays tested.
What I'd do differently
Write the trust boundary down first, before choosing the embedding approach. Once "only the iframe talks to the backend" was written down, most later decisions were easy.