Add chat to a React app
@ethora/chat-component is a React chat UI (room list and chat room) that talks to the Ethora API and XMPP server. This guide installs it, connects it to your app on Ethora Cloud and signs your users in without an Ethora login screen.
Using Claude Code or Codex?
Let Claude Code or Codex do it
Our open-source skill for Claude Code and Codex runs the steps on this page for you: it installs the SDK, signs in to Ethora from your terminal to create or pick an app, writes the env files and, with your OK, adds the sign-in token route to your backend. Then it checks the setup.
/plugin marketplace add dappros/chat-ethora-skill /plugin install ethora-skill@ethora
codex plugin marketplace add dappros/chat-ethora-skill codex plugin add ethora-skill@ethora
Then ask your agent to add Ethora chat to this app. Cursor and other agents: npx skills add dappros/chat-ethora-skill.
Prerequisites
reactandreact-dom^18.3.1 or ^19 (peer dependencies). The XMPP client, Redux and styled-components are bundled.- An Ethora app: its
appIdand app token (JWT eyJ…). To sign users in you also need the app secret (admin panel → app → Settings → API), which stays on your server. - A backend route that can sign a JWT (any language).
Install
npm i @ethora/chat-component
# yarn add / pnpm add / bun add @ethora/chat-component also workImport the base stylesheet once, and remove <React.StrictMode>. In 26.9.x its double-run effects start the XMPP connection twice: "Connecting…" takes about 30 s and messages stay "sending…". Vite and CRA templates add StrictMode by default.
import { createRoot } from 'react-dom/client';
import '@ethora/chat-component/dist/main.css';
import App from './App';
createRoot(document.getElementById('root')!).render(<App />); // no <StrictMode>Render the chat
Chat must be inside XmppProvider, which owns the XMPP connection. The chat fills its container, so give the container a height.
import { Chat, XmppProvider } from '@ethora/chat-component';
export default function ChatPage() {
return (
<div style={{ height: '100vh' }}>
<XmppProvider>
<Chat />
</XmppProvider>
</div>
);
}With no config the chat uses the Ethora Cloud defaults and, with nobody signed in, shows its built-in login form. That is enough to check the UI. The form currently sends a fixed appId, so only accounts of the base Ethora app (register here) can sign in with it, not users of your app.
Connect to your Ethora app
Build one memoized config and pass the same object to XmppProvider and Chat. These are the Ethora Cloud values:
import { useMemo } from 'react';
import { Chat, XmppProvider } from '@ethora/chat-component';
import type { IConfig } from '@ethora/chat-component';
export default function ChatPage({ clientJwt }: { clientJwt: string }) {
const config = useMemo<IConfig>(() => ({
appId: import.meta.env.VITE_ETHORA_APP_ID,
customAppToken: import.meta.env.VITE_ETHORA_APP_TOKEN, // "JWT eyJ…"
baseUrl: 'https://api.chat.ethora.com',
xmppSettings: {
devServer: 'wss://xmpp.chat.ethora.com/ws', // web: full wss:// URL
host: 'xmpp.chat.ethora.com',
conference: 'conference.xmpp.chat.ethora.com',
},
initBeforeLoad: true,
refreshTokens: { enabled: true },
jwtLogin: { enabled: true, token: clientJwt },
}), [clientJwt]);
return (
<div style={{ height: '100vh' }}>
<XmppProvider config={config}>
<Chat config={config} />
</XmppProvider>
</div>
);
}initBeforeLoad: truemakesXmppProviderthe only place that opens the connection. Do not callclient.login()yourself.baseUrlis the API origin; a trailing/v1is also accepted.- Without
baseUrlandxmppSettingsthe chat falls back to Ethora Cloud. Keeping them explicit makes a later move to a self-hosted backend a config change. - Env prefixes:
VITE_ETHORA_*(Vite),NEXT_PUBLIC_ETHORA_*(Next.js),REACT_APP_ETHORA_*(CRA).
Sign users in with jwtLogin
Your backend signs a short-lived JWT (HS256) with the app secret, payload { data: { type: 'client', appId, userId } }. The chat exchanges it at POST /v1/users/client and opens already signed in. userId is your own id for the user. Two rules: the secret never goes into a browser bundle, and the user must already exist in the app. The exchange does not create users and answers 401 USER_NOT_FOUND. The backend part is in Authentication.
Render the chat only once the token exists:
import { useEffect, useState } from 'react';
import ChatPage from './ChatPage';
export default function ChatRoute() {
const [token, setToken] = useState<string | null>(null);
useEffect(() => {
fetch('/api/ethora/token', { credentials: 'include' }) // your route: { token }
.then((res) => res.json())
.then((data: { token: string }) => setToken(data.token));
}, []);
return token ? <ChatPage clientJwt={token} /> : null;
}With refreshTokens: { enabled: true } the SDK keeps its own session, so a new client JWT is only needed on a cold start. When your app signs out, call logoutService.performLogout() first. It clears the chat store and disconnects XMPP.
Next.js
The component uses window and WebSocket, so it must render only in the browser. Load it with next/dynamic and ssr: false from a client component:
'use client';
import dynamic from 'next/dynamic';
const EthoraChat = dynamic(() => import('./EthoraChat'), { ssr: false });
export default EthoraChat;import EthoraChat from '@/components/EthoraChatNext';
export default function Page() {
return <main style={{ height: '100vh' }}><EthoraChat /></main>;
}components/EthoraChat.tsx holds the ChatRoute/ChatPage code above, reading process.env.NEXT_PUBLIC_ETHORA_*. Also:
- Set
reactStrictMode: falseinnext.config.js. The App Router turns StrictMode on in development. - Put the token route in
app/api/ethora/token/route.ts(Pages Router:pages/api/ethora/token.ts).ETHORA_APP_SECRETis server-only and never gets theNEXT_PUBLIC_prefix.
Unread badge outside the chat
useUnread works anywhere inside XmppProvider. For a badge in a global header, wrap the app root with <XmppProvider config={config}> and render <Chat config={config} /> on the chat page. The connection then also survives navigation.
import { useUnread } from '@ethora/chat-component';
export function UnreadBadge() {
const { totalCount, displayTotal, loading } = useUnread();
if (loading || totalCount === 0) return null;
return <span className="badge">{displayTotal}</span>;
}totalCount leaves out muted and hidden rooms, unreadByRoom maps room JID to count, displayTotal is the total as a string (with + when a count is capped), and loading stays true until the first counts settle.
Troubleshooting
| Symptom | Fix |
|---|---|
useXmppClient must be used within an XmppProvider | Render Chat and useUnread inside XmppProvider. |
| "Connecting…" for about 30 s, then messages "sending…" / "Not delivered" | Remove <React.StrictMode> (Next.js: reactStrictMode: false). |
Two wss://…/ws connections or duplicate messages | One memoized config for provider and chat, initBeforeLoad: true, no manual client.login(). |
Login screen although jwtLogin is set, or 401 USER_NOT_FOUND | Render Chat only after the token exists; create the user in the app first (/v1/users/batch). |
Next.js: window is not defined or hydration errors | 'use client' plus next/dynamic(..., { ssr: false }). |
For more detail, set useStoreConsoleEnabled: true and check the browser console.
Next steps
- Customize the chat UI: colors, fonts, layout, admin panel appearance.
- Authentication: sign the client JWT and sync users on your backend.
- Rooms: create rooms for your users and open one room directly.
Stuck or planning something bigger?
Book 15 minutes with the team, or open an issue on GitHub. Self-hosting and enterprise questions are welcome.