D
Deno

help

How to create a broadcast system with streams and oak?

ASAntonio Sampaio10/1/2022
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();
// 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);
}
}
// 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;
});
// oak-router
router.get("/stream", ({ request, response }) => {
const stream = radio.addListener();

response.headers.set("Content-Type", "audio/mpeg");
response.body = stream;
});

Looking for more? Join the community!