toninhoT
Denoβ€’4y ago
toninho

How to create a broadcast system with streams and oak?

i'm trying to create a simple "web-radio" to learn more about streams with deno and oak.
i'm stuck on the part that oak read a stream and send to response
let me show you some code
// nextSong() {}
const file = await Deno.open(this.currentSong, { read: true });
const fileSize = (await file.stat()).size;
const throttle = bandwidthThrottleGroup.createBandwidthThrottle(fileSize);
const stream = readableStreamFromReader(file).pipeThrough(throttle);

for await (const chunk of stream) {
    this.broadcast(chunk);
}

file.close();
throttle.destroy();

// broadcast() {}
private broadcast(chunk: Uint8Array) {
  for (const listener of this.listeners) {
    listener.write(chunk);
  }
}

// oak-router
router.get("/stream", ({ request, response }) => {
  const stream = radio.addListener();

  response.headers.set("Content-Type", "audio/mpeg");
  response.body = stream;
});
Was this page helpful?