how to send files in the js sdk? <https://github.c...
# ask-ai
n
how to send files in the js sdk? https://github.com/livekit/client-sdk-js
t
Thanks for reaching out, rohit. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
You're asking how to send files with the LiveKit JavaScript SDK. Use the LocalParticipant
sendFile
method and register a byte-stream handler to receive files. 1) Sending (from a browser or Node):
Copy code
// file from an <input> or a Blob
const file = fileInput.files?.[0];
if (file) {
  const info = await room.localParticipant.sendFile(file, {
    mimeType: file.type,
    topic: 'my-topic',
    onProgress: (p) => console.log('progress', Math.round(p * 100) + '%'),
  });
  console.log('sent file stream id', info.id);
}
You can also pass a file path or
Blob
. Options: • `topic`: string to target receivers • `onProgress(progress: number)`: 0–1 progress callback • `mimeType`: override MIME type 2) Receiving:
Copy code
room.registerByteStreamHandler('my-topic', async (stream) => {
  // stream contains metadata and a readable stream of bytes
  // consume and save/display the file as needed
});
If you want, tell me: • whether you're in browser or Node • example file type/size and I’ll provide a concrete receive-handler example for that environment. Sources: Sending files & bytes | LiveKit docs
n
does livekit rtc have it? Check if @alert-easter-19258/rtc-node has been updated with sendFile() in a newer version
t
You're asking if the Node SDK (@livekit/rtc-node) includes
sendFile()
. Yes —
@livekit/rtc-node
provides
sendFile()
on
LocalParticipant
(same usage as the browser SDK). Example (Node):
Copy code
const info = await room.localParticipant.sendFile('path/to/file.jpg', {
  topic: 'my-topic',
  onProgress: (p) => console.log('progress', Math.round(p * 100) + '%'),
});
console.log('sent file stream id', info.id);
To receive files, use:
Copy code
room.registerByteStreamHandler('my-topic', async (stream) => {
  // consume stream bytes and save/display the file
});
Would you like a full Node receive-handler example that writes the incoming stream to disk and shows progress? Sources: Sending files & bytes | LiveKit docs
n
what version is this
t
The
sendFile()
method was added in @livekit/rtc-node starting with version 0.8.0. Sources: Sending files & bytes | LiveKit docs
n
sendFile() doesn't exist in @livekit/rtc-node@0.11.1. We have to use publishData().
t
Thanks — I see
sendFile()
isn't available in your
@livekit/rtc-node@0.11.1
setup and you need to use
publishData()
instead. I can help with a concise pattern for sending files with
publishData()
and receiving them with
registerDataReceivedHandler
. Do you want a Node.js example that: • chunks a file, publishes chunks with metadata (filename, chunk index, total, mime), and shows progress, and • a receive handler that reassembles the chunks and writes the file to disk? If yes, tell me whether you're using plain Node (no bundler) and the file sizes you expect (so I can recommend chunk size).