how to get message id
# ask-ai
b
how to get message id
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Are you asking about LiveKit text streams (chat) message IDs or a different message type? If you mean LiveKit text streams: • When you send a text stream, the returned
info
contains the stream ID (e.g.,
info.stream_id
in Python). • When receiving, the handler gives
reader.info.id
(e.g.,
console.log(reader.info.id)
). Which platform/language are you using so I can give a concrete code example? Sources: Sending text | LiveKit docs
b
i need message id from both backend python and front end react
t
You're asking how to get the LiveKit text stream (message) ID from both a Python backend and a React frontend. Below are minimal, concrete examples for each. Backend — Python (sending text) 1. When you send a text stream from the server, the returned
info
contains the stream ID.
Copy code
# async example using LiveKit Python SDK
info = await room.local_participant.send_text("Hello", topic="chat")
print(info.stream_id)  # message / stream ID
Frontend — React (receiving text) 1. When registering a text stream handler in the browser, the
reader.info.id
contains the stream ID.
Copy code
// assume `room` is a connected LiveKit Room
room.registerTextStreamHandler("chat", async (reader, participantIdentity) => {
  console.log("Received stream ID:", reader.info.id);
  // read the text if needed
  const msg = await reader.readText();
  console.log("Text:", msg);
});
Frontend — React (sending text) 1. When sending from the client, the returned
info
object includes the ID:
Copy code
const info = await room.localParticipant.sendText("Hello", { topic: "chat" });
console.log("Sent stream ID:", info.id);
Notes • The field name differs slightly between SDKs: Python uses
stream_id
on the returned `info`; JS uses
id
on the returned
info
and
reader.info.id
when receiving. • If you want to persist or reference IDs across backend and frontend, capture the returned ID when sending and store it in your database or return it in your API response. Would you like full examples showing how to persist these IDs in a database or correlate a sent ID from backend to frontend?
b
@session.on("conversation_item_added") def on_conversation_item_added(event: ConversationItemAddedEvent):
t
You're handling the
conversation_item_added
event and want the message ID in both backend (Python) and frontend (React). Here's exactly how to get it and how to deliver it to the frontend. Backend — Python (inside your event handler) • Extract the ID from the event item:
Copy code
@session.on("conversation_item_added")
def on_conversation_item_added(event: ConversationItemAddedEvent):
    message_id = event.item.id
    role = event.item.role
    text = event.item.text_content
    # persist or forward the ID + content as needed
    print(f"Message ID: {message_id}, role: {role}, text: {text}")
Options to get the ID to the React frontend • Send it via your backend API: store the message (ID + content) in your DB and return it from an API endpoint the React app calls or polls. • Push it in real time via WebSocket / Server-Sent Events: send the event payload including
item.id
to connected clients as soon as you receive
conversation_item_added
. • Embed the ID in a text stream sent through LiveKit: include the ID in the JSON payload when sending text streams so receivers see it (e.g.,
sendText(JSON.stringify({ id, text, sender }))
) and parse it in React. Frontend — React • If you receive via your own socket/API: ◦ Parse the incoming payload and read
id
. • If you receive via LiveKit text stream with embedded ID:
Copy code
room.registerTextStreamHandler("chat-messages", async (reader) => {
  for await (const chunk of reader) {
    const msg = JSON.parse(chunk);
    console.log("Message ID:", msg.id, "Text:", msg.text);
  }
});
Which delivery method do you prefer (API polling, WebSocket push, or embedding IDs in LiveKit text streams)? I can give a concrete React + backend example for the one you pick. Sources: Events and error handling | LiveKit docs