when i use sendFile method in expo, "file.stream i...
# ask-ai
b
when i use sendFile method in expo, "file.stream is not a function" error occurs
t
Thanks for reaching out, RikuKomiya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! The error "file.stream is not a function" pops up because the
sendFile
method in the LiveKit JavaScript SDK expects a web
File
object, which comes with a handy
.stream()
method. The tricky part is, this method isn’t available in Expo or React Native since they don’t support the web’s File API. But don’t worry! You can work around this by using the
streamBytes
method to send your file data manually. Here’s how: 1. First, read the file using
expo-file-system
and convert it to binary data. 2. Then, send the file in chunks using
streamBytes
. Here’s a quick example to get you started:
Copy code
import { FileSystem } from 'expo-file-system';
import { localParticipant } from 'livekit-client';

async function sendFileViaStreamBytes(filePath, fileName, topic) {
  const base64String = await FileSystem.readAsStringAsync(filePath, {
    encoding: FileSystem.EncodingType.Base64,
  });

  const byteCharacters = atob(base64String);
  const byteNumbers = new Array(byteCharacters.length);
  for (let i = 0; i < byteCharacters.length; i++) {
    byteNumbers[i] = byteCharacters.charCodeAt(i);
  }
  const fileBytes = new Uint8Array(byteNumbers);

  const writer = await localParticipant.streamBytes({
    name: fileName,
    topic: topic,
    totalSize: fileBytes.length,
  });

  const CHUNK_SIZE = 15000;
  for (let i = 0; i < fileBytes.length; i += CHUNK_SIZE) {
    const chunk = fileBytes.slice(i, i + CHUNK_SIZE);
    await writer.write(chunk);
  }

  await writer.close();
}
This way, you can send files in Expo without needing the web-specific
File
API. If you hit any snags or have questions, just give me a shout—I’m here to help! Sources: examples/data-streams/index.ts | lib/src/participant/local.dart | src/room/data-stream/outgoing/OutgoingDataStreamManager.ts | livekit-rtc/livekit/rtc/participant.py | Tests/LiveKitTests/DataStream/DataStreamTests.swift
b
streamBytesだとagentからのimage Fileをchatのcontextに入れる方法は変わりますか?
t
Hey there! If you want to include an image file from an agent into the chat context using
streamBytes
, here’s a friendly walkthrough of how it works: you’ll stream the image bytes, gather them up in the agent, and then encode them as a base64 data URL to add to the chat context. Let me break it down for you: 1. Stream the image from the frontend: Use
streamBytes
to send the image in chunks. Here’s an example:
Copy code
const writer = await localParticipant.streamBytes({
  topic: 'images',
  name: file.name,
  mimeType: file.type,
  totalSize: file.size,
});

const CHUNK_SIZE = 15000;
for (let i = 0; i < fileBytes.length; i += CHUNK_SIZE) {
  const chunk = fileBytes.slice(i, i + CHUNK_SIZE);
  await writer.write(chunk);
}
await writer.close();
1. Register a byte stream handler in the agent: The agent will handle the incoming byte stream and gather all the data like this:
Copy code
async def _image_received(self, reader, participant_identity):
    image_bytes = bytes()
    async for chunk in reader:
        image_bytes += chunk

    mime_type = reader.info.mimeType or "image/png"
    chat_ctx = self.chat_ctx.copy()
    chat_ctx.add_message(
        role="user",
        content=[
            ImageContent(
                image=f"data:{mime_type};base64,{base64.b64encode(image_bytes).decode('utf-8')}"
            )
        ],
    )
    await self.update_chat_ctx(chat_ctx)
This way, the image gets streamed smoothly, processed just right, and added to the chat context. If anything’s unclear or you want me to walk you through more examples, just let me know—I’m happy to help! Sources: Vision | LiveKit Docs | agent/main.py | tests/test_llm.py | Sending files & bytes | LiveKit Docs | Sending files & bytes | LiveKit Docs