deskcord
v1.6.0
Published
The chat widget customers embed on their site. Preact + plain CSS in a shadow root, one IIFE file (`dist/deskcord.min.js`, ~51 KB gzipped) published to npm as `deskcord` and served from cdn.deskcord.chat.
Readme
Deskcord widget
The chat widget customers embed on their site. Preact + plain CSS in a shadow root, one IIFE file (dist/deskcord.min.js, ~51 KB gzipped) published to npm as deskcord and served from cdn.deskcord.chat.
Embed
<script>
window.deskcord = {
settings: {
app_id: "YOUR_DISCORD_SERVER_ID",
// theme: "auto", // "light" (default), "dark", or "auto" (follows the visitor's system)
// locale: "fr", // widget language; defaults to the page's <html lang>
},
};
</script>
<script async src="https://cdn.deskcord.chat/deskcord.min.js"></script>Colors, position, titles and the offline message come from the server's /customize-widget settings.
window.deskcord also exposes openChatbox(), closeChatbox(), identify(user), logout(), setTheme(theme) and setLocale(locale) once the script has loaded.
Theme
The widget never reads your page's colors (it lives in a shadow root). It has its own color scheme:
"light"(default),"dark", or"auto", which follows the visitor's system setting and updates live when it changes.- If your site has its own dark-mode switch, call
window.deskcord.setTheme("light" | "dark" | "auto")whenever it changes. It applies immediately, no reload.
Language
The widget speaks English, French, German, Spanish, Brazilian Portuguese, Italian, Dutch, Polish, Japanese and Turkish (src/i18n/locales/). It picks, in order: settings.locale, the page's <html lang>, the visitor's browser languages, then English. Regions fall back to their language (fr-CA → French, pt-PT → Brazilian Portuguese).
- Only the widget's own texts are translated: messages, and the titles and offline message set with
/customize-widget, show as written. - Sites that switch language without a reload call
window.deskcord.setLocale("de"). It applies immediately. - Adding a language: copy
en.ts, translate it, register it insrc/i18n/index.ts. The type checker andi18n.test.tscatch missing keys, placeholders and plural forms.
Identify logged-in users
window.deskcord.identify({
name: "Ada",
email: "[email protected]",
avatarUrl: "https://example.com/ada.png", // optional, https only, shown in Discord
userId: "42", // optional, with userHash: same conversations on every device
userHash: "…", // HMAC-SHA256(identity secret, userId), computed on the site's server
metadata: { plan: "pro", signupDate: "2026-01-04", mrr: 49 }, // optional, Pro servers only
});
window.deskcord.logout(); // on logout: forgets the user on this browser- Skips the "who should we reply to?" step. Without
userId/userHash, conversations stay tied to the browser. - The secret comes from
/identity-verification enablein Discord. It must never reach the browser. - To identify before the first render, pass the same object as
window.deskcord.settings.userin the embed snippet. - Server-rendered sites should print
user: nullwhen nobody is logged in: it ends a verified session left over from a logout that didn't calllogout(). - Call it on every page load: verified sessions last 24h, and one without a token is dropped.
metadatagives support context in Discord (top of each new conversation, and/customer-infoinside a thread). It's a flat object: values are strings (200 characters at most), numbers, booleans ornull; 20 keys at most, keys of 40 characters at most, about 1 KB in all. What doesn't fit is dropped with a console warning. Eachidentify()replaces the previous metadata (leave it out to clear it), andlogout()forgets it.- Metadata is not covered by
userHash: the visitor can edit it in the browser, so Discord always labels it "Unverified, set by the website". Don't put secrets in it. Servers on the Free plan ignore it (nothing errors). - Conversations started anonymously in the browser are carried over to the user. Calling
identify()with another user (orlogout()) wipes the previous user's conversations from the browser.
Develop
npm install
npm run dev:mock # mock Deskcord API on :3999 (no Discord needed)
npm run dev # then open http://localhost:5173/?api=mock (&theme=dark|auto)Without ?api=mock, the dev page talks to a local API on http://localhost:10001.
The mock plays the Discord side through POST localhost:3999/__mock/<action>: reply ({"text": "…"}), typing, image, edit, delete, handoff, close, reopen, offline, online, drop (cuts the live connection), auto-reply ({"on": false}), reset.
npm test # unit tests (sync model, persistence, rich text, colors)
npm run build
npm run e2e # drives the built widget in headless Chrome against the mock (CHROME=/path/to/chrome)How it works
src/state/session.tsowns everything that talks to the server: boot, the socket, the outbox, uploads, identify/logout, read state, persistence. The UI only reads the store and calls its actions.- The server is the source of truth. A fetched conversation replaces the cached one; messages are matched by Discord id (
src/state/model.ts), never by guessing from content. - Messages being sent live in an outbox, sent one at a time so they reach Discord in order, and survive a reload. Server data that may contain them waits until they're confirmed, so nothing shows twice.
- After every reconnect (and when the tab comes back after a while) the current conversation is fetched again: events missed while offline aren't lost.
- Unread state is a single date on the server's clock; the badge is derived from it.
- The current page (origin + path, never the query or hash) goes with each message and socket connect (
src/lib/page.ts): on Pro, the API links back to it when it emails a reply the visitor missed. - Data lives in
localStorageunderdeskcord:<app_id>. Visitors from widgets before 1.0 (cookiediscord-widget) are migrated on their first visit.
