Real time online radio

Hi. I was wondering if anyone had experience working in building online internet radios with Nodejs. I’m trying to build a real-time audio stream across multiple clients.

This means that Node should request the audio file from S3, play it on the server and output the stream to the client so that every single user who connects hears the same thing.

I have tried to used node-lame, streammachine and socket-stream but I’m still struggling with the output. Closest I’ve got was the following.

var fs = require('fs');
var lame = require('lame');
var Speaker = require('speaker'); 

fs.createReadStream(process.argv[2])
  .pipe(new lame.Decoder)
  .on('format', console.log)
  .pipe(new Speaker);

What it does is just grabbing the sample file from the command line and output it into my speakers. I need this to output into the client that has an tag.

Also, I’ve tried the streammachine but found it very inflexible when adding a playlist.

I appreciate any input!

Did you try asking on the NodeJs gitter channel?

Yup. Got some good feedback but nothing related to building live radio streams.

Try this:

const fs = require('fs');
const http = require('http');

const musicStream = fs.createReadStream(process.argv[2]);

http
  .createServer((request, response) => {
    response.writeHead(200, {
      'Content-Type': 'audio/mpeg',
    });
    musicStream.pipe(response);
  })
  .listen(8080);

Edit:

Also check out this stackoverflow thread: https://stackoverflow.com/questions/3955103/streaming-audio-from-a-node-js-server-to-html5-audio-tag

1 Like

Thanks @Implaier. I appreciate the help :slight_smile:

Is there a Radio API that a radio station provides?

I know this isn’t totally on topic to your post, but it is something to think about if your working with a radio.

There’s no radio API that I found that does the stream. I solved this problem by creating a streaming server using icecast to create a constant stream of audio. Then I used node to get the
info into the client.

1 Like

If you cant figure out your issue, try to @ some of the biggest/ best helping people on the forum.

Thanks for the info but I actually solved it already by setting up Icecast on an EC2 instance and then mounting S3 to the server. :slight_smile:

I will definetly use your advice for my next question.

1 Like