Skip to content

Frameworks and Templates

<m8ty-chat> is a standard custom element, so it works in every front-end framework and in server-rendered pages. Load the script once, render the element, and — for delegated shares — set the getTicket property on the element (it is a function, so it cannot be passed as an HTML attribute).

The examples assume a delegated share and a ticket endpoint POST /api/m8ty/ticket as described in Delegated Chat. For a public share omit everything related to getTicket.

Tab For
React, Angular, Vue Single-page applications built with a bundler
Velocity Server-rendered Java pages (Apache Velocity templates)

Both integration types are covered: web component and iframe. Use the one enabled on your share.

For React, Angular and Vue, use this shared helper:

m8ty-ticket.ts
export async function fetchTicket(nonce: string): Promise<{ ticket: string }> {
const response = await fetch("/api/m8ty/ticket", {
method: "POST",
credentials: "same-origin",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ nonce }),
});
if (!response.ok) throw new Error("ticket request failed");
return response.json();
}

and add the script tag once to index.html:

<script type="module" src="https://rag.example.com/embed-assets/v1/m8ty-chat.js" crossorigin="anonymous"></script>
import { useEffect, useRef } from "react";
import type { M8tyChat } from "./m8ty-chat"; // copied m8ty-chat.d.ts
import { fetchTicket } from "./m8ty-ticket";
declare module "react" {
namespace JSX {
interface IntrinsicElements {
"m8ty-chat": React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> & {
share: string;
locale?: string;
theme?: string;
};
}
}
}
export function SupportChat() {
const ref = useRef<M8tyChat>(null);
useEffect(() => {
const chat = ref.current;
if (!chat) return;
chat.getTicket = fetchTicket;
const onError = (event: Event) => console.warn((event as CustomEvent).detail);
chat.addEventListener("m8ty-error", onError);
return () => chat.removeEventListener("m8ty-error", onError);
}, []);
return <m8ty-chat ref={ref} share="shr_XXXXXXXXXXXXXXXXXXXXXXXX" locale="de" style={{ display: "block", height: 600 }} />;
}

Public share: render the <iframe> from Option B: iframe as plain markup in any framework or template — no script and no ticket are needed.

Delegated share: the chat inside the iframe asks your page for a ticket via postMessage. connectIframe (from the same m8ty-chat.js script, also available as window.M8tyEmbed.connectIframe) answers these requests with your getTicket function and returns a function that disconnects it.

import { useEffect, useRef } from "react";
import { fetchTicket } from "./m8ty-ticket";
const FRAME_URL = "https://rag.example.com/embed/shr_XXXXXXXXXXXXXXXXXXXXXXXX?locale=de";
export function SupportChatFrame() {
const ref = useRef<HTMLIFrameElement>(null);
useEffect(() => {
let disconnect: (() => void) | undefined;
let cancelled = false;
void customElements.whenDefined("m8ty-chat").then(() => {
const iframe = ref.current;
if (cancelled || !iframe) return;
iframe.src = FRAME_URL; // set src and connect in the same step
disconnect = window.M8tyEmbed.connectIframe(iframe, { getTicket: fetchTicket });
});
return () => {
cancelled = true;
disconnect?.();
};
}, []);
return (
<iframe ref={ref} title="Chat" allow="clipboard-write" referrerPolicy="strict-origin"
style={{ width: "100%", height: 600, border: 0 }} />
);
}

Keep referrerpolicy="strict-origin": the iframe uses it to determine your page origin, and connectIframe answers only that iframe and only towards the Ragty origin.

  • Mount the chat (element or iframe) only after the user is logged in (delegated) and unmount it on logout. Unmounting aborts running requests; for the iframe call the function returned by connectIframe.
  • Several <m8ty-chat> elements on one page are supported (for example two different shares).
  • Changing the share attribute restarts the chat with the new share.
  • Server-side rendering: render the element as plain HTML; it activates in the browser once the script is loaded. Set getTicket on the client only.