I want to add to sendText some metadata, like usin...
# ask-ai
c
I want to add to sendText some metadata, like using reference to sent text like mini context of "question" in chat. How is it possible?
t
Thanks for reaching out, Babken. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm happy to help you add metadata to
sendText
in LiveKit! You can do this by using the
attributes
field within the
SendTextOptions
parameter. This lets you attach key-value metadata to your text message, which recipients can then access when they handle incoming text streams. Here’s a simple example using the Flutter SDK:
Copy code
final testAttributes = {'userId': '12345', 'sessionId': 'abc123', 'priority': 'high'};

final info = await room.localParticipant?.sendText('Test message with metadata',
    options: SendTextOptions(
      topic: 'chat-metadata',
      attributes: testAttributes, // Attach metadata here
    ));
And on the receiving side, you can handle it like this:
Copy code
room.registerTextStreamHandler('chat-metadata', (TextStreamReader reader, String participantIdentity) async {
  final text = await reader.readAll();
  print('Received attributes: ${reader.info?.attributes}');
  // Access specific metadata
  print('User ID: ${reader.info!.attributes['userId']}');
});
This feature is also available in other SDKs like Python and JavaScript/TypeScript. If you'd like, I’d be more than happy to provide examples for those or assist you further. Just let me know! Sources: lib/src/participant/local.dart | livekit-rtc/livekit/rtc/participant.py | src/room/participant/LocalParticipant.ts | Sending text | LiveKit docs