Node.js Media Server

4.2.4 · active · verified Sun Apr 19

Node-Media-Server is an open-source, high-performance, and low-latency live streaming server built with Node.js. The current stable version is 4.2.4. It focuses on providing a robust platform for RTMP (Real-Time Messaging Protocol), HTTP-FLV, and WS-FLV streaming, with enhanced support for modern codecs like HEVC, VP9, and AV1 in its v4 release. This version introduced a comprehensive REST API for management, JWT-based authentication, real-time monitoring, and session management, making it suitable for modern streaming applications. It differentiates itself from alternatives like Nginx RTMP or enterprise solutions by being a Node.js-native, extensible, and lightweight solution ideal for rapid development and integration into JavaScript ecosystems. While it doesn't specify a fixed release cadence, the project shows active development and regular updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart sets up a Node-Media-Server instance with RTMP and HTTP-FLV streaming, a REST API with JWT authentication, static file serving, and stream recording. It also includes basic event logging and environment variable usage for sensitive configuration.

import NodeMediaServer from 'node-media-server';
import * as fs from 'fs';

// Ensure media and html directories exist for recording and static files
const mediaRoot = './media';
const recordPath = `${mediaRoot}/record`;
const htmlRoot = './html';

if (!fs.existsSync(mediaRoot)) fs.mkdirSync(mediaRoot);
if (!fs.existsSync(recordPath)) fs.mkdirSync(recordPath, { recursive: true });
if (!fs.existsSync(htmlRoot)) fs.mkdirSync(htmlRoot);
// Create a dummy index.html for static server to function
if (!fs.existsSync(`${htmlRoot}/index.html`)) {
  fs.writeFileSync(`${htmlRoot}/index.html`, '<h1>Node-Media-Server Static Content</h1><p>This is a placeholder page.</p>');
}

const config = {
  rtmp: {
    port: 1935,
    chunk_size: 60000,
    gop_cache: true,
    ping: 30,
    ping_timeout: 60
  },
  http: {
    port: 8000,
    mediaroot: mediaRoot, // Directory for recorded files or static content
    allow_origin: '*',
    api: true, // Enable REST API
    api_user: process.env.NMS_API_USER ?? 'admin',
    api_pass: process.env.NMS_API_PASS ?? 's3cr3tP@ssw0rd'
  },
  auth: {
    play: false,
    publish: false,
    secret: process.env.NMS_JWT_SECRET ?? 'superSecretKey!HighlySecure!ChangeMe!',
    jwt: {
      expiresIn: '1h',
      algorithm: 'HS256',
      users: [
        {
          username: process.env.NMS_ADMIN_USERNAME ?? 'admin',
          password: process.env.NMS_ADMIN_PASSWORD ?? 'p@ssw0rdF0rAdm1n' // IMPORTANT: Hash this in production!
        }
      ]
    }
  },
  record: {
    path: recordPath,
    append_file: true // Append to existing file if stream restarts
  },
  static: {
    router: '/static',
    root: htmlRoot
  }
};

const nms = new NodeMediaServer(config);

nms.on('postPublish', (id, StreamPath, args) => {
  console.log(`[NodeEvent on postPublish] Stream started: ${StreamPath}`);
  // Example: Log stream details or integrate with other services
});

nms.on('doneConnect', (id, args) => {
  console.log(`[NodeEvent on doneConnect] Client disconnected: ${id}`);
});

try {
  nms.run();
  console.log('Node-Media-Server is running.');
  console.log(`RTMP server listening on port ${config.rtmp.port}`);
  console.log(`HTTP server with API listening on port ${config.http.port}`);
  console.log('Access static files at /static');
  console.log('Publish stream via RTMP, e.g., rtmp://localhost:1935/live/streamkey');
  console.log('Play stream via HTTP-FLV, e.g., http://localhost:8000/live/streamkey.flv');
} catch (error) {
  console.error('Failed to start Node-Media-Server:', error);
}

view raw JSON →