Components / Chat
@peerkit-dev/chat
Session messaging with links, timestamps, and live status.
Use ChatController, the <peerkit-chat> element, or the React component. Messages travel on a real PeerKit data channel for the active session only — nothing is stored after it ends.
JavaScript and Web Components
npm install @peerkit-dev/chat-core @peerkit-dev/sdkReact
npm install @peerkit-dev/chat @peerkit-dev/sdk react react-domLive demo
PeerKitChat
Starting a real data session…
Chat
Provide a session or invitation
Messages in this session appear here
Installation
Install @peerkit-dev/chat-core for vanilla JavaScript or the <peerkit-chat> element. Install @peerkit-dev/chat when the page is React. The React package depends on the core package and re-exports ChatController.
JavaScript and Web Components
npm install @peerkit-dev/chat-core @peerkit-dev/sdk
React
npm install @peerkit-dev/chat @peerkit-dev/sdk react react-dom
Vanilla JavaScript
ChatController holds the view state. subscribe and getSnapshot are how a page reads it. Call sync() after construction, and dispose() when the view goes away.
import { ChatController } from "@peerkit-dev/chat-core";
const chat = new ChatController(() => ({
invitationUrl: "https://peerkit.dev/s#inv_…",
}));
chat.subscribe(() => {
const snapshot = chat.getSnapshot();
console.log(snapshot.statusLabel, snapshot.messages.length);
});
chat.sync();
chat.send("hello");Web Component
Importing @peerkit-dev/chat-core does not register the element. Import @peerkit-dev/chat-core/element, or load dist/element.global.js from a script tag. The element renders in light DOM.
<script type="module">
import "@peerkit-dev/chat-core/element";
</script>
<peerkit-chat></peerkit-chat>
<script type="module">
const chat = document.querySelector("peerkit-chat");
chat.invitationUrl = "https://peerkit.dev/s#inv_…";
</script>React
Prefer an existing PeerKitSession when pairing components so they share one connection. Messaging and file transfers are live, session-based operations — PeerKit does not store conversations or files.
import { PeerKitChat } from "@peerkit-dev/chat";
import type { PeerKitSession } from "@peerkit-dev/sdk";
export function Chat({ session }: { session: PeerKitSession }) {
return (
<PeerKitChat
session={session}
onMessage={(message) => console.log(message.direction, message.text)}
onError={(error) => console.error(error.message)}
/>
);
}React props
The custom element uses the same names as properties. className and style are React. Theme either surface with the CSS variables below.
| Prop | Purpose |
|---|---|
session | Advanced. Existing SDK session. Recommended for shared use with the other component. |
invitationUrl / signalingUrl + token | Simple join when the component should create and connect its own session. |
labels | Override UI copy for titles, buttons, and empty states. |
className / style | Root styling. Theme with --peerkit-chat-* variables on .pk-chat or a wrapper. |
onMessage, onError, onConnectionChange, onReady | Callbacks for session activity and failures. |
Theming
PeerKitChat ships neutral defaults. Override --peerkit-chat-* variables in your CSS. Fonts inherit from the host app unless you set --peerkit-chat-font.
.my-chat {
--peerkit-chat-accent: #0d9488;
--peerkit-chat-button-bg: #0d9488;
--peerkit-chat-button-ink: #ffffff;
--peerkit-chat-bg: #f8fafc;
--peerkit-chat-mine: #ccfbf1;
}Useful tokens include accent, button background/ink/hover, background, surface, ink, muted, line, radius, message bubbles (mine/theirs), status-ready, danger, and font.