Skip to content

Instantly share code, notes, and snippets.

@pdaug
Created October 21, 2025 16:08
Show Gist options
  • Save pdaug/8ae2ec39b5ba495e5f66ea5c842adc6e to your computer and use it in GitHub Desktop.
Save pdaug/8ae2ec39b5ba495e5f66ea5c842adc6e to your computer and use it in GitHub Desktop.
const playlistFolder = path.resolve("./playlist");
const playlistTracks = [
"track1.mp3",
"track2.mp3",
"track3.mp3",
].map(folder => path.join(playlistFolder, folder));
let radioStream = new PassThrough();
let currentIndex = 0;
function startRadio() {
const filePath = playlist[currentIndex];
const readStream = fs.createReadStream(filePath);
console.log(path.basename(filePath));
readStream.pipe(radioStream, { end: false });
readStream.on('end', () => {
currentIndex = (currentIndex + 1) % playlist.length;
startRadio();
});
readStream.on('error', err => {
console.error('Error: ', err);
currentIndex = (currentIndex + 1) % playlist.length;
startRadio();
});
}
startRadio();
app.get('/radio', (req, res) => {
res.setHeader('Content-Type', 'audio/mpeg');
res.setHeader('Cache-Control', 'no-cache');
req.socket.setTimeout(0);
// Cada cliente recebe dados do stream atual
const clientStream = new PassThrough();
radioStream.pipe(clientStream);
clientStream.pipe(res);
req.on('close', () => {
clientStream.unpipe(res);
clientStream.destroy();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment