Docs
React SDK

useMessages

Send room chat and read the provider's local history.

Import

React
import { useMessages } from '@relayrtc/react'

Requirements

Call this hook inside RelayProvider, in a React function component or custom hook. A joined room is needed for live data and actions.

Returns

FieldTypeMeaning
messagesreadonly RoomTextMessage[]Messages collected by this provider.
send(text: string) => Promise<RoomTextMessage>Send a text message.
clear() => voidClear only this client's history.

Example

React
const { messages, send } = useMessages()
 
return (
  <div>
    {messages.map((message) => <p key={message.messageId}>{message.text}</p>)}
    <button onClick={() => void send('Hello').catch(console.error)}>Say hello</button>
  </div>
)

Options and behavior

Sending requires messages:send. Text must be nonblank and no longer than 4,000 characters. A message contains messageId, roomId, participantId and text. History is local, limited by messageHistoryLimit, and is not persisted across reloads.