Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions packages/react/src/hooks/useChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,36 @@ import { useConnectionState } from './useConnectionStatus';

/**
* The `useChat` hook provides chat functionality for a LiveKit room.
* It returns a simple `send` function to send chat messages and an array of `chatMessages` to hold received messages.
* It also returns a `update` function that allows you to implement message-edit functionality.
*
* @remarks
* Message history is not persisted and will be lost if the component is refreshed.
* You may want to persist message history in the browser, a cache or a database.
*
* @returns An object containing:
* - `chatMessages` - Array of received chat messages
* - `send` - Function to send a new message
* - `update` - Function to edit an existing message
* - `isSending` - Boolean indicating if a message is currently being sent
*
* @example
* ```tsx
* function ChatComponent() {
* const { chatMessages, send, isSending } = useChat();
*
* return (
* <div>
* {chatMessages.map((msg) => (
* <div key={msg.timestamp}>
* {msg.from?.identity}: {msg.message}
* </div>
* ))}
* <button disabled={isSending} onClick={() => send("Hello!")}>
* Send Message
* </button>
* </div>
* );
* }
* ```
* @public
*/
export function useChat(options?: ChatOptions) {
Expand Down
26 changes: 20 additions & 6 deletions packages/react/src/prefabs/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,29 @@ export interface ChatProps extends React.HTMLAttributes<HTMLDivElement>, ChatOpt
}

/**
* The Chat component adds a basis chat functionality to the LiveKit room. The messages are distributed to all participants
* in the room. Only users who are in the room at the time of dispatch will receive the message.
*
* The Chat component provides ready-to-use chat functionality in a LiveKit room.
* Messages are distributed to all participants in the room in real-time.
*
* @remarks
* - Only users who are in the room at the time of dispatch will receive messages
* - Message history is not persisted between sessions
* - Requires `@livekit/components-styles` to be imported for styling
*
* @example
* ```tsx
* <LiveKitRoom>
* <Chat />
* </LiveKitRoom>
* import '@livekit/components-styles';
*
* function Room() {
* return (
* <LiveKitRoom data-lk-theme="default">
* <Chat />
* </LiveKitRoom>
* );
* }
* ```
*
* For custom styling, refer to: https://docs.livekit.io/reference/components/react/concepts/style-components/
*
* @public
*/
export function Chat({
Expand Down
Loading