Add a live Discord chat to a React or Next.js site

Updated

There is no Wizcord npm package, and you do not need one. The chat is either an iframe (inline in your page) or one script tag (a floating chat button). Both work in React and Next.js with a few lines. Get your channel's code from the Embed your chat card on your Wizcord dashboard first: it gives you the channel ID and checks that the bot can read the channel. If you have not set Wizcord up yet, start with how to embed a live Discord channel on your website.

The examples below are in plain JavaScript. We built and ran them with Next.js 14.2 and React 18.3, and with Next.js 16.2 and React 19.2. Treat them as a starting point, not a package.

An inline chat component

The chat is a plain iframe whose address carries the options. This component builds the same address the dashboard does:

// components/WizcordChat.jsx
// Props: channelId (the Discord channel ID, required); channelList (the channel list beside
// the chat, off as in the dashboard); showPinned; theme ('dark' or 'light'); accent (six hex
// digits, with or without '#'); loadOlder; threads; height (pixels, never taller than 80% of
// the screen); maxWidth (pixels, never wider than its container); lazy (load only when near
// the screen); title.

export function wizcordSrc({
  channelId, channelList = false, showPinned = false, theme = 'dark',
  accent, loadOlder = false, threads = false,
}) {
  let src = `https://wizcord.io/iframe?channelId=${encodeURIComponent(channelId)}`;
  if (!channelList) src += '&menu=false';
  if (showPinned) src += '&show_pinned=true';
  if (theme === 'light') src += '&theme=light';
  const a = /^#?([0-9a-fA-F]{6})$/.exec((accent ?? '').trim());
  if (a) src += `&accent=${a[1].toUpperCase()}`;
  if (loadOlder) src += '&load_older=true';
  if (threads) src += '&threads=true';
  return src;
}

export default function WizcordChat(props) {
  const { height = 600, maxWidth = 800, lazy = false, title = 'Discord chat' } = props;
  return (
    <iframe
      src={wizcordSrc(props)}
      title={title}
      width="100%"
      height={height}
      loading={lazy ? 'lazy' : undefined}
      style={{ display: 'block', maxWidth, maxHeight: '80vh', border: 0 }}
    />
  );
}

Use it anywhere:

<WizcordChat channelId="YOUR_CHANNEL_ID" theme="light" accent="#5865F2" />

Each option is explained in the embed reference. Changing channelId or an option changes the address, and the iframe reloads with the new settings. In TypeScript, give the props a type with the same names; channelId is a string and the rest are optional.

The floating chat button

Next.js

Load the script once, in the root layout, with next/script:

// app/layout.jsx
import Script from 'next/script';

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://wizcord.io/iframe/embed.js"
          data-channel-id="YOUR_CHANNEL_ID"
          strategy="lazyOnload"
        />
      </body>
    </html>
  );
}

next/script copies data-… attributes onto the script tag, which is where the loader reads its options. Add data-theme, data-label and the other attributes the same way. strategy="afterInteractive" also works and shows the button sooner. You do not need beforeInteractive (see below).

Put it in the root layout if you want the button on every page. The loader has no way to remove the button, so if you load it on one page only, it stays on screen after a client-side navigation to another page, until a full reload. The same goes for changing the channel: the loader ignores a second copy of itself, so a different channel needs a page reload.

React without Next.js (Vite and similar)

Put the script tag in index.html, before </body>:

<script src="https://wizcord.io/iframe/embed.js" data-channel-id="YOUR_CHANNEL_ID" async></script>

If the channel ID comes from your app, add the script from a component instead:

'use client'; // needed in Next.js; Vite ignores it

import { useEffect } from 'react';

const SRC = 'https://wizcord.io/iframe/embed.js';

export default function WizcordButton({ channelId, label }) {
  useEffect(() => {
    if (document.querySelector(`script[src="${SRC}"]`)) return; // added already
    const s = document.createElement('script');
    s.src = SRC;
    s.async = true;
    s.setAttribute('data-channel-id', channelId);
    if (label) s.setAttribute('data-label', label);
    document.body.appendChild(s);
  }, [channelId, label]);
  return null;
}

There is no clean-up function, on purpose: the button cannot be removed, and the check at the top stops React's development mode, which runs effects twice, from adding it twice. The button keeps the channel and label it started with. Changing the props later has no effect until the page is reloaded.

Opening the chat from your own button

The script adds window.WizcordChat with open(), close() and toggle(). In a client component:

'use client';

export default function OpenChatButton() {
  return (
    <button type="button" onClick={() => window.WizcordChat?.open()}>
      Chat with us
    </button>
  );
}

The ?. matters: the object exists only once the script has run with a valid channel ID. A click before then does nothing.

In TypeScript, declare the object once in a .d.ts file that has no import or export line, so that it adds to the global Window type:

interface Window {
  WizcordChat?: { open(): void; close(): void; toggle(): void };
}

Server rendering and hydration

The inline component is safe to render on the server. It has no hooks and reads nothing from the browser, so the server and the browser produce the same <iframe> and hydration has nothing to disagree about. In the Next.js App Router it works as a Server Component, with no 'use client'. To keep it that way:

  • Build the address only from props. Do not add anything that differs between server and browser, such as window.location, a random value or the time.
  • Do not render the iframe only when typeof window !== 'undefined'. That gives different HTML on the server and in the browser, which is exactly what causes hydration warnings.
  • You do not need next/dynamic with ssr: false. It only delays the chat and moves your layout when it appears.
  • Give the iframe a height (the component does) so the page does not jump when it loads.

The floating button never takes part in hydration. next/script with afterInteractive or lazyOnload, and useEffect, add the script only once React has started hydrating the page. The loader then adds its own <wizcord-chat> element at the end of <body>. React does not manage that element, so it neither removes it nor warns about it. beforeInteractive makes the script load before any Next.js code. Next.js meant it for critical scripts such as cookie consent managers, and a chat button does not need that.

Lazy loading

Inline chat. lazy adds loading="lazy" to the iframe. The browser then waits until the chat is close to the screen before loading it. Browsers only do this when JavaScript is on. Use it when the chat is far down a long page: visitors who never scroll that far do not load it or open its live connection. Leave it off when the chat is near the top: it would load at once anyway, and lazy loading only adds a small delay. One side effect: your dashboard's We saw your embed check only sees a load once someone has scrolled to the chat and stayed a few seconds.

Floating button. It is already lazy. The script only draws the button; the chat itself loads when the panel is first opened (or at once, with data-open="true"). With strategy="lazyOnload" Next.js also waits until the page has finished loading before adding the script.

Content-Security-Policy

If your site sets a Content-Security-Policy, allow https://wizcord.io in frame-src (both kinds) and in script-src (the button). If you use a nonce, pass it to <Script nonce={…}>: in browsers without constructable stylesheets, the loader puts the same nonce on the <style> element it adds.

Wizcord is free while it is in beta, and paid plans are coming. Create an account, connect your server, and copy your channel's code from the dashboard.

Wizcord is free while it is in beta.

Create an accountRead the documentation