Building a Radio Server with Node.js

Are you ready to create your own online radio station? With Node.js, you can build a fully functional radio server that streams audio data to clients. In this article, we’ll take you through the process of creating a radio server, complete with a control panel, audio streaming, and a simple webpage.

Control Panel

The control panel is the heart of our radio server. We’ll build it using Node.js and the neo-blessed library, which allows us to create a terminal GUI. Our control panel will have four main sections: Playlist, Queue, NowPlaying, and Controls. Each section will be a neo-blessed box, which can be stylized and positioned in the terminal.

We’ll start by defining the main screen of the view layer, with a render method that we’ll use periodically to render the entire view. We’ll attach some keys (Ctrl+C, Esc) to terminate the app if needed. Next, we’ll implement the different sections of the view, each with its own box.

Playlist Box

The Playlist box will contain a list of all available songs. We’ll implement a basic class for this, which will be extended by other classes later. The Playlist box will have a scroll method, which allows users to navigate the items with the help of an illusion made by different coloring of blurred and focused items.

Queue Box

The Queue box will be used for storing the list of songs queued up for streaming. We’ll extend the TerminalItemBox class to implement the Queue box. This class will not only be in charge of the view layer but also contain all the functionalities for streaming and piping data to all the consumers (i.e., clients).

NowPlaying Box

The NowPlaying box will have only one item: the song that is currently played. We’ll implement it by creating an instance and inserting it into the main view.

Controls Box

The Controls box will contain a hardcoded list of keyboard keybindings. We’ll stylize it according to our needs, passing config options during instantiation.

Stream Magic

Now that we have our control panel ready, it’s time to implement the streaming logic. We’ll use the Node.js Stream API to achieve one-to-many transfer of data. We’ll create a transform stream (which is both a readable and a writable) and a timer function to slow down the streaming of the chunks.

We’ll use the throttle package from npm to limit the streaming of the chunks to be no faster than the bytes per second we provide. We’ll determine the bitrate for every song that we stream using the @dropb/ffprobe package from npm.

Server

Finally, we’ll create an HTTP server using Hapi.js. We’ll implement an HTTP endpoint that will register the client as a consumer and add it to our queue’s _sinks map. We’ll also start streaming the data back to the client.

Our server will also serve static files, providing a handy webpage with some radio controls. The audio element on the webpage will make a request to the endpoint when the page loads.

Wrapping Up

That’s it! We’ve built a fully functional radio server with a control panel, audio streaming, and a simple webpage. You can find the entire repo for this article on GitHub. This was a fun project, and we hope you enjoyed it too!

Leave a Reply

Your email address will not be published. Required fields are marked *