How can I persist chat messages? I know how to loa...
# ask-ai
c
How can I persist chat messages? I know how to load them, but not how to save them after receiving them.
t
Thanks for reaching out, Robert. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
c
Sure.
t
Hey there! Could you share which storage solution you're using (like local storage, a database, or Firebase) or which programming language you're working with? That way, I can whip up a detailed example that fits perfectly with what you need.
c
Let's say an in-memory database for simplicity.
t
Using an in-memory database like Redis is a fantastic choice when you want something simple and fast. Let me walk you through how you can save chat messages with Redis: 1. Set Up Redis: First, install and run a Redis server. You can use libraries like
ioredis
or
redis
in Node.js to connect and interact with Redis easily. 2. Save Messages: Whenever a new message comes in, just store it in Redis using a unique key—something like the room ID or user ID works great. 3. Retrieve Messages: When you need to get messages, like when someone joins a chat room, you can fetch them straight from Redis. Here’s a simple example in Node.js to get you started:
Copy code
const Redis = require('ioredis');
const redis = new Redis();

// Save a message
function saveMessage(roomId, message) {
  const key = `chat:${roomId}`;
  redis.rpush(key, JSON.stringify(message));
}

// Retrieve messages
async function getMessages(roomId) {
  const key = `chat:${roomId}`;
  const messages = await redis.lrange(key, 0, -1);
  return messages.map(msg => JSON.parse(msg));
}

// Example usage
saveMessage('room1', { sender: 'user1', content: 'Hello!', timestamp: Date.now() });
getMessages('room1').then(messages => console.log(messages));
If you want, I’m here to help you dive deeper or troubleshoot anything along the way. Just let me know!
c
Yes, but how do you save them in an AgentSession?
t
To save chat messages in an
AgentSession
, there are a few different ways you can go about it, depending on what fits your needs best: 1. Using a Key-Value Store: You could save messages in a key-value store like Redis or another in-memory database. For instance, if you’re working with TypeScript,
kv.hmset
and
kv.zadd
are handy for storing messages and linking them to a user or session. 2. Event Listeners: If you’re using Python, you can tap into events like
user_speech_committed
or
agent_stopped_speaking
to catch and log messages as they happen. 3. ChatContext: Another option is to add messages to a
ChatContext
object, which keeps track of the conversation history during the session—great for temporary storage. If you want, I can share some detailed examples for any of these methods. Or if you have a particular programming language or storage option in mind, just let me know! Sources: Sending text | LiveKit Docs | Sending text | LiveKit Docs | client/app/actions.ts | examples/voice_agents/realtime_load_chat_history.py | VoicePipelineAgent | LiveKit Docs
c
The VoicePipelineAgent is from v0.x, I'm on v1.x
t
In LiveKit v1.x, you can save chat messages in an
AgentSession
by setting up a custom solution. Here are a couple of ways you might do this: 1. Using ChatContext: You can load chat history into the session with
ChatContext
. Here’s a quick example:
Copy code
chat_ctx = llm.ChatContext.empty()
for item in chat_history:
    chat_ctx.add_message(role=item["role"], content=item["content"])

session = AgentSession()
agent = Agent(
    instructions="You are a helpful assistant.",
    llm=openai.realtime.RealtimeModel(modalities=["text"]),
    chat_ctx=chat_ctx,
)
await session.start(agent=agent, room=ctx.room)
1. Save Chat History to a File: Another way is to use the
add_shutdown_callback
method to save the chat history when the session ends:
Copy code
def save_chat_history(session: AgentSession):
    history = session.history
    chat_history = [
        {"role": msg.role.name, "content": msg.text_content}
        for msg in history if isinstance(msg, llm.ChatMessage)
    ]
    with open("chat_history.json", "w") as f:
        json.dump(chat_history, f, indent=2)

session.add_shutdown_callback(save_chat_history)
If you want, I’d be happy to walk you through these methods in more detail or help out with any specific part of the implementation. Just let me know! Sources: Sending text | LiveKit Docs | ChatContext | LiveKit Agents | Session recording and transcripts | LiveKit Docs
c
i want to save the message as soon as I receive them
t
Hey there! To save messages right as they come in with LiveKit v1.x, you can set up event listeners that catch and store those messages in real-time. Here’s a simple way to do it: 1. Listen for Incoming Messages: Just use the right event listener to know when a message pops up. 2. Save the Message: Then, save that message right away in whatever storage you prefer—whether that’s a database, a file, or even in-memory. Here’s a quick example in Python to get you started:
Copy code
@agent.on("user_speech_committed")
def on_user_message(msg: llm.ChatMessage):
    # Save the message immediately
    save_message({
        "role": "user",
        "content": msg.text_content,
        "timestamp": datetime.now().isoformat()
    })

@agent.on("agent_stopped_speaking")
def on_agent_message(msg: llm.ChatMessage):
    # Save the message immediately
    save_message({
        "role": "assistant",
        "content": msg.text_content,
        "timestamp": datetime.now().isoformat()
    })

def save_message(message):
    # Example: Save to a file or database
    with open("chat_history.json", "a") as f:
        f.write(json.dumps(message) + "\n")
If you want, I’d be happy to help you tweak this for your specific storage setup or programming language. Just let me know!
c
Does it work with chat message received on
lk.chat
topic?
t
Absolutely! You can save chat messages received on the
lk.chat
topic. Just use the
setupDataMessageHandler
function to listen for incoming messages and then store them wherever works best for you. Here’s a simple example in JavaScript to get you started:
Copy code
const { messageObservable } = setupDataMessageHandler(room, 'lk.chat');

messageObservable.subscribe((message) => {
  // Save the message to your storage solution
  saveMessageToDatabase({
    id: message.id,
    sender: message.from?.identity,
    content: message.message,
    timestamp: new Date(message.timestamp)
  });
});
If you want, I’d be happy to help you tweak this for a specific storage option or programming language. Just let me know! Sources: Sending text | LiveKit Docs
c
Agent in v1.x does not have
on
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!
c
There isn't any info about it