bulky-fish-79784
07/14/2025, 8:12 PMtall-airline-16756
07/14/2025, 8:12 PMtall-airline-16756
07/14/2025, 8:13 PMuseChat
hook maintains a messages
array containing all sent and received messages, each with a timestamp, sender info, and message content. You can display or further process this array as needed. Here’s a basic usage example:
import { useChat } from '@livekit/components-react';
export default function Chat() {
const { messages, send, isSending } = useChat();
// messages contains all chat history
}
For custom logging (such as saving to a file or database), you can extend the message handling logic to write each message to your preferred destination. The same approach applies to the Android SDK, where the Chat
class maintains a message log.
Note: By default, message history is not persisted across page reloads. If you need persistent logs, you should implement storage in the browser, a cache, or a backend database.
Let me know if you need a specific code example or guidance for a particular platform.
Sources: livekit-compose-components/src/main/java/io/livekit/android/compose/chat/Ch... | Add chat to your React app | LiveKit Docs | VoicePipelineAgent | LiveKit Docs | examples/other/chat-stream-receiver.py | Text and transcriptions | LiveKit Docsbulky-fish-79784
07/14/2025, 8:13 PMbulky-fish-79784
07/14/2025, 8:14 PMtall-airline-16756
07/14/2025, 8:16 PMimport os
import logging
import asyncio
from livekit import rtc
# Set up basic logging
logging.basicConfig(level=<http://logging.INFO|logging.INFO>,
handlers=[
logging.FileHandler("chat_example.log"),
logging.StreamHandler(),
],
)
logger = logging.getLogger(__name__)
async def on_chat_message_received(reader: rtc.TextStreamReader, participant_identity: str):
full_text = await reader.read_all()
<http://logger.info|logger.info>("Received chat message from %s: '%s'", participant_identity, full_text)
async def main(room: rtc.Room):
active_tasks = []
def _handle_chat_stream(reader, participant_identity):
task = asyncio.create_task(on_chat_message_received(reader, participant_identity))
active_tasks.append(task)
task.add_done_callback(lambda _: active_tasks.remove(task))
room.set_text_stream_handler("chat", _handle_chat_stream)
await room.connect(URL, TOKEN)
<http://logger.info|logger.info>("connected to room %s", room.name)
if __name__ == "__main__":
loop = asyncio.get_event_loop()
room = rtc.Room(loop=loop)
async def cleanup():
await room.disconnect()
loop.stop()
asyncio.ensure_future(main(room))
try:
loop.run_forever()
finally:
loop.close()
Just a heads up - you'll need to plug in your own URL
and TOKEN
to get this running. One thing to keep in mind: LiveKit doesn't automatically save chat history. So if you want to keep those conversations around long-term, you'll want to set up a database or another storage solution.
Sources: Sending text | LiveKit Docs | livekit-rtc/livekit/rtc/log.py