Install guides

Add live chat to a React app

Add Dinghy live chat to a React app built with Vite or an older Create React App setup. The tag goes in index.html, and any component can open it.

In a React single-page app, the chat tag goes in index.html, not in a component. It loads once, and the chat stays put while your router changes pages. This works for Vite and for older Create React App projects. For Next.js, see the Next.js guide.

Get your tag

It's in the Dinghy operator app, under Settings > Install:

HTML
<script src="https://chat.example.com/widget/loader.js" data-key="wk_your_public_key" async></script>

Add your site under Allowed sites in Settings > Install, like https://app.example.com. For local work, add your dev server too. That's http://localhost:5173 for Vite and http://localhost:3000 for Create React App, unless you changed the port.

Paste it in index.html

With Vite, index.html is at the root of your project. With Create React App, it's public/index.html. Paste the tag just above </body>:

HTML
  <body>
    <div id="root"></div>
    <script type="module" src="/src/main.tsx"></script>
    <script src="https://chat.example.com/widget/loader.js" data-key="wk_your_public_key" async></script>
  </body>

The build leaves the tag alone, because it points at another address.

Don't add the script from a useEffect. In development, React's Strict Mode runs effects twice, and each run adds a script. The chat script doesn't check for a copy of itself, so you'd get two chat buttons.

If you'd rather install from npm, the @dinghy-chat/widget package adds the tag once, no matter how often it runs, and gives you typed calls. See Install it from npm.

Route changes

The chat adds itself to <body>, outside the element React renders into. When React Router or any other router changes the page, nothing reloads, so the chat stays where it is. Visitors can keep typing in an open chat while they change pages.

Open the chat from a component

The tag adds a Dinghy object to the page. But the script loads with async, so it might not be there yet when your component first renders. If the script hasn't loaded, it queues the call, and the script runs it when it starts:

TypeScript
// src/dinghy.ts
declare global {
  interface Window {
    Dinghy?: { q?: unknown[][]; open?: () => void; close?: () => void };
  }
}

export function openChat() {
  if (window.Dinghy?.open) window.Dinghy.open();
  else (window.Dinghy ??= { q: [] }).q?.push(["open"]);
}

Then use it anywhere:

TypeScript
import { openChat } from "./dinghy";

export function HelpButton() {
  return <button onClick={openChat}>Chat with us</button>;
}

Want only your own button? Add data-launcher="hidden" to the tag in index.html. Dinghy's button goes away, and openChat() still opens the chat. Dinghy.on("unread", (count) => ...) gives you the unread count for your own badge. See Control it from your page.

Show who is chatting

If your app has accounts, your server can sign a short token. Then the chat follows the user across devices and your team sees who they are. The Next.js guide has a full React example, and the server part works with any backend. The token rules are in Show who is chatting.

When the user signs out, call Dinghy.reset(), so the next person on that computer can't read the chat.

If you use a Content Security Policy

Allow your Dinghy address in script-src, connect-src and img-src. Dinghy doesn't need unsafe-inline.

Keep reading