npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-web-radio

v1.0.0

Published

A simple, easily configurable web radio implementation for Express.js. Read MP3 files, and stream them on app routes.

Downloads

5

Readme

Please Note: Currently, this package only supports MP3 files. Support for other audio formats may be added in the future.

Working Example (using my own music 😅): https://radio.diamonddigital.dev/willtda

NPM

Downloads Discord Server

Features

  • Fast and Efficient | Express Web Radio is built for production, and is heavily optimized for speed and memory usage. Express Web Radio keeps track of connected clients and readstreams, and frees up resources when they are no longer needed.

  • 🤹‍♂️ Unlimited Stations | Express Web Radio's class-based design allows you to create as many stations as you want, and attach them to different routes in your app without any interference.

  • 📦 Easy Integration | Express Web Radio is designed with developers in mind, and provides a super easy API to get started with. Express Web Radio also provides a simple middleware function to serve the radio on a route.

Install Package

Express Web Radio can be installed with the following command:

npm install express-web-radio --save

Setup and Usage

To create a radio in your Express application, you must first create a new instance of the WebRadio class. This class takes in an optional options object as a parameter, which can be used to configure the radio.

const WebRadio = require("express-web-radio");

const radio = new WebRadio({
  // Options go here
});

The following options are available:

  • audioDirectory - (string)

    • The directory where your audio files are stored. Defaults to "./audio".

    • Note: The directory path is relative to the current working directory of your application. (process.cwd())

  • bitrate - (number)

    • Set a strict bitrate at which audio files should be played. If unspecified, @dropb/ffprobe will be used to dynamically determine the bitrate for each file. If an error occurs while using @dropb/ffprobe, the bitrate will fallback to 128000 (128kbps).

    • Note: @dropb/ffprobe requires ffmpeg to be installed on your system to function properly. This option should only be used as a last resort if you cannot get ffmpeg installed and working correctly.

  • loop - (boolean)

    • Decides whether the songs in the audioDirectory should be played indefinitely. Defaults to true.
  • shuffle - (boolean)

    • Decides whether the songs in the audioDirectory should play in a random order. Defaults to false.
  • logFn - (function)

    • A function that will be called whenever the radio logs something. Defaults to console.log.

    • Example: logFn: (msg) => console.log(msg)

Starting the Radio

Once you have created a radio instance, you can start the radio by calling the start() method.

const WebRadio = require("express-web-radio");

const radio = new WebRadio({
  // Options go here
});

radio.start(); // Start the radio

Allowing Connections to the Radio

To allow clients to connect to the radio, you must first create a route in your Express application. Then, you can use the connect() method to create a middleware function that will allow clients to connect to the radio stream.

Note: You may notice a considerable delay between the time you request the route and the time the audio starts playing. This is because throttling is used to ensure smooth playback across all connected clients.

//assuming you have already created an Express app and a radio instance...

app.get("/stream", radio.connect()); //allow clients to connect to the radio stream

Stopping the Radio

To stop the radio stream and disconnect all connected clients, you can call the stop() method.

This method also has an optional graceful parameter, which can be used to decide whether the radio should wait for the current song to finish playing before stopping. This parameter defaults to false.

//assuming you have already created an Express app and a radio instance that is running...

//stop the radio stream immediately
radio.stop();

//stop the radio stream gracefully (wait for the current song to finish playing)
radio.stop(true);

Full Example

Here's a full example of how you can use Express Web Radio in your application:

const express = require("express");
const app = express();

const WebRadio = require("express-web-radio");

const radio = new WebRadio({
    audioDirectory: "./audio", //set the directory where audio files are stored
    loop: true, //loop the audio files
    shuffle: true, //shuffle the play order of the audio files
    logFn: (msg) => console.log(`[Radio]: ${msg}`) //log radio messages to the console
});

radio.start(); //start the radio stream

app.get("/stream", radio.connect()); //allow clients to connect to the radio stream

app.listen(3000, () => console.log("Server running on port 3000!"));

Contact Us