Can I build a text to text chat with livekit?
# ask-ai
w
Can I build a text to text chat with livekit?
t
Thanks for reaching out, Ilya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Absolutely, you can build a text-to-text chat application with LiveKit! It offers great features like real-time text streams, handy pre-built React components (like
Chat
and
useChat
), and useful methods such as
sendText
and
registerTextStreamHandler
to help you send and receive messages smoothly. Just a heads-up: LiveKit doesn’t store messages by default, so if you want to keep a history of chats, you’ll need to set up your own way to save them. If you want, I’d be happy to share more details or examples to help you get going—just let me know! Sources: Text and transcriptions | LiveKit Docs | examples/basic_text_stream/src/main.rs | examples/other/text_only.py | Sending text | LiveKit Docs | Sending text | LiveKit Docs
w
send me some details
t
Here are some details to help you build a text-to-text chat application with LiveKit: 1. Real-Time Text Streams: LiveKit lets you send and receive text messages instantly using text streams. You can even link each stream to a topic, which helps keep the conversation organized. 2. Pre-Built Components: If you’re working with React, LiveKit offers handy pre-built components like
Chat
and hooks like
useChat
to make setting up your chat interface a breeze. 3. Sending Text: Sending messages is straightforward with the
sendText
method. Here’s a quick example:
Copy code
const text = 'Hello, how are you?';
const info = await room.localParticipant.sendText(text, {
  topic: 'chat',
});
console.log(`Sent text with stream ID: ${info.id}`);
4. Receiving Text: To get incoming messages, you just register a handler using
registerTextStreamHandler
. Like this:
Copy code
room.registerTextStreamHandler('chat', async (reader, participantInfo) => {
  const message = await reader.readAll();
  console.log(`Received message from ${participantInfo.identity}: ${message}`);
});
5. Message Persistence: One thing to keep in mind—LiveKit doesn’t save messages automatically. So, if you want to keep a history, you’ll need to set up your own storage solution. 6. Cross-Platform Support: LiveKit works across lots of platforms like JavaScript, Swift, Python, and Rust, so you’ve got plenty of flexibility no matter what environment you’re working in. If you have any questions or want some specific examples, just let me know—I’m here to help!
m
can you use this with SMS
t
LiveKit makes it easy for participants to chat in real-time using its text streaming features like
sendText
and
streamText
. That said, it doesn’t have built-in support for SMS services just yet. If you want to connect SMS with LiveKit, you’d need to set up a bridge between LiveKit’s text streams and an SMS service API (think Twilio or Nexmo). This means forwarding messages back and forth using webhooks or APIs. I’d be happy to help you get this set up! Just let me know what you’re aiming for or which SMS service you’re thinking about, and we can take it from there.
w
I think it’s overkill for sms. livekit allows you to hold connection and stream text/voice to client, in sms you don’t need all of this
m
yes could you do that?
yes but the agents framework though @wooden-dream-294 how do you have an sms agent that uses the same livekit agents framework?
t
You're absolutely right—LiveKit’s real-time streaming features, like keeping connections alive and streaming text or voice, really shine in interactive apps. SMS, on the other hand, works differently since it’s asynchronous and doesn’t keep persistent connections. If you’re focusing just on SMS communication, using a dedicated SMS service like Twilio or Nexmo might be a smoother and more cost-effective route. These services are built specifically for sending and receiving SMS without needing real-time connections. Feel free to reach out if you want a hand setting up an SMS service or if you have other scenarios where LiveKit’s capabilities could be a great match. I’m here to help!