DEV Community

Gingertool
Gingertool

Posted on AI-assisted

React Chat Widget: Add a Free Live Chat to a React or Next.js App

A React chat widget for live support doesn't need an npm package or a chat backend you build yourself: load a hosted widget's script once at the root of your app, and drive it from your components through a small global API. This post shows how with SealReply, a hosted live chat whose Free plan has no time limit, for a Vite/CRA app and for Next.js (App Router and Pages Router).

Why a hosted widget instead of building one

Building a support chat means WebSockets, an agent dashboard, offline handling, notifications and storage. For most apps that's weeks of work that has nothing to do with the product. A hosted widget gives you all of it for one script tag, and your React code only has to do two things:

  • load the script once, so client-side navigation doesn't create a second widget;
  • talk to it from components (open it from your own button, react to new messages, tell it who's signed in).

SealReply is a good fit for this because its widget is built with single-page apps in mind: it's about 38 KB, loads async, follows route changes made with history.pushState, and exposes a small window.SealReply API. The Free plan includes 1 agent, 1 website and 300 chats a month, with no card.

Step 1: Get the embed code

Sign up, add your site's address (the widget only loads on that domain), and copy the embed code. It's one line:

<script src="https://cdn.sealreply.com/widget.js" data-site-key="sk_…" async></script>
Enter fullscreen mode Exit fullscreen mode

The site key isn't a secret, so it can sit in your code or in a public env variable.

Step 2a: Plain React (Vite or CRA)

The simplest correct place is index.html, just before </body>:

<!-- index.html -->
<body>
  <div id="root"></div>
  <script type="module" src="/src/main.tsx"></script>
  <script src="https://cdn.sealreply.com/widget.js" data-site-key="sk_…" async></script>
</body>
Enter fullscreen mode Exit fullscreen mode

It sits outside React, so re-renders, Strict Mode double-mounting and route changes never touch it.

Step 2b: Next.js App Router

Next.js has no HTML file, so use next/script in the root layout. Your data-* attributes are forwarded to the script element:

// app/layout.tsx
import Script from "next/script";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://cdn.sealreply.com/widget.js"
          data-site-key="sk_…"
          strategy="afterInteractive"
        />
      </body>
    </html>
  );
}
Enter fullscreen mode Exit fullscreen mode

Use afterInteractive to show the chat button early, or lazyOnload if your Lighthouse score matters more. A script in the root layout loads once, even as users navigate with <Link>.

Step 2c: Next.js Pages Router

Same idea, in pages/_app.tsx (not _document.tsx):

// pages/_app.tsx
import type { AppProps } from "next/app";
import Script from "next/script";

export default function App({ Component, pageProps }: AppProps) {
  return (
    <>
      <Component {...pageProps} />
      <Script src="https://cdn.sealreply.com/widget.js" data-site-key="sk_…" strategy="afterInteractive" />
    </>
  );
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Check there's exactly one widget

Open your deployed app, click through a few routes, then run this in the console:

document.querySelectorAll('script[src*="widget.js"]').length; // 1
window.SealReply?.version; // a version string
Enter fullscreen mode Exit fullscreen mode

One tag and a version means it's loaded once and ready. Send yourself a message and answer it from the dashboard to check both ends.

Step 4: Open the chat from your own button

Add data-launcher="none" to the script tag to hide the default corner button, then open the chat from any component. The script is async, so guard with optional chaining:

"use client";

export function ContactButton() {
  return (
    <button type="button" onClick={() => window.SealReply?.open()}>
      Chat with us
    </button>
  );
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Show an unread count

The widget fires a sealreply:ready event on window when it's ready, and lets you subscribe to new messages:

"use client";
import { useEffect, useState } from "react";

export function ChatButton() {
  const [unread, setUnread] = useState(0);

  useEffect(() => {
    const onMessage = () => setUnread((n) => n + 1);
    const subscribe = () => window.SealReply?.on("message", onMessage);
    if (window.SealReply) subscribe();
    else window.addEventListener("sealreply:ready", subscribe, { once: true });
    return () => {
      window.removeEventListener("sealreply:ready", subscribe);
      window.SealReply?.off("message", onMessage);
    };
  }, []);

  return (
    <button type="button" onClick={() => window.SealReply?.open()}>
      Chat with us{unread > 0 ? ` (${unread})` : ""}
    </button>
  );
}
Enter fullscreen mode Exit fullscreen mode

The cleanup matters: without off, every remount adds another listener.

Step 6: Tell the chat who's signed in (safely)

Passing a plain user ID to a chat widget lets anyone change it in DevTools and chat as someone else. SealReply avoids this by checking an HMAC-SHA256 signature of the ID that your server makes with a secret from the dashboard. In a SPA, call identify after sign-in and reset on sign-out:

// identityHash comes from your backend, never computed in the browser
window.SealReply?.identify({
  external_id: String(user.id), // sign and send the exact same string
  identity_hash: identityHash,
  name: user.name,
  email: user.email,
});

// on sign-out
window.SealReply?.reset();
Enter fullscreen mode Exit fullscreen mode

Agents then see a verified customer with one history across devices. A missing or wrong signature never breaks the chat; the visitor just stays anonymous.

Wrapping up

For a React app, live chat comes down to: one script loaded once at the root, a couple of guarded calls from client components, and a server-signed identity if you have accounts. You can use SealReply free with no card and no time limit. The full Next.js write-up, including TypeScript types for window.SealReply, is here: How to add live chat to Next.js.

Disclosure: the Gingertool team also works on SealReply. This post was written with AI assistance.

Top comments (0)