Simple livekit chat app where it can log user mess...
# ask-ai
b
Simple livekit chat app where it can log user message and ouptut message too
t
Thanks for reaching out, Sahab. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
LiveKit provides chat components that already log both user and output messages. For example, in React, the
useChat
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:
Copy code
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 Docs
b
in python
give me example in python
t
Hey there! Let me walk you through a friendly example of a LiveKit chat app in Python that helps you keep track of messages. It's super simple and logs both user and output messages, so you'll always know what's happening.
Copy code
import 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