Binary Data

Send and receive ArrayBuffer data natively over WebSocket using live.binary().

Server

// src/live/upload.js
import { live } from 'svelte-realtime/server';

export const uploadAvatar = live.binary(async (ctx, buffer, filename) => {
  await storage.put(`avatars/${ctx.user.id}/${filename}`, buffer);
  return { url: `/avatars/${ctx.user.id}/${filename}` };
}, { maxSize: 5 * 1024 * 1024 }); // reject payloads over 5MB (default: 10MB)
OptionDefaultDescription
maxSize10 * 1024 * 1024 (10MB)Maximum payload size in bytes. Rejects with an error if exceeded.

Client

<script>
  import { uploadAvatar } from '$live/upload';

  async function handleFile(e) {
    const file = e.target.files[0];
    const buffer = await file.arrayBuffer();
    const { url } = await uploadAvatar(buffer, file.name);
  }
</script>

<input type="file" accept="image/*" onchange={handleFile} />

Binary RPCs bypass JSON serialization - the raw ArrayBuffer is sent as a WebSocket binary frame.

Was this page helpful?