{"library":"node-media-server","title":"Node.js Media Server","description":"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.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-media-server"],"cli":null},"imports":["import NodeMediaServer from 'node-media-server';","import type { Config } from 'node-media-server';","import type { NmsEvent } from 'node-media-server';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import NodeMediaServer from 'node-media-server';\nimport * as fs from 'fs';\n\n// Ensure media and html directories exist for recording and static files\nconst mediaRoot = './media';\nconst recordPath = `${mediaRoot}/record`;\nconst htmlRoot = './html';\n\nif (!fs.existsSync(mediaRoot)) fs.mkdirSync(mediaRoot);\nif (!fs.existsSync(recordPath)) fs.mkdirSync(recordPath, { recursive: true });\nif (!fs.existsSync(htmlRoot)) fs.mkdirSync(htmlRoot);\n// Create a dummy index.html for static server to function\nif (!fs.existsSync(`${htmlRoot}/index.html`)) {\n  fs.writeFileSync(`${htmlRoot}/index.html`, '<h1>Node-Media-Server Static Content</h1><p>This is a placeholder page.</p>');\n}\n\nconst config = {\n  rtmp: {\n    port: 1935,\n    chunk_size: 60000,\n    gop_cache: true,\n    ping: 30,\n    ping_timeout: 60\n  },\n  http: {\n    port: 8000,\n    mediaroot: mediaRoot, // Directory for recorded files or static content\n    allow_origin: '*',\n    api: true, // Enable REST API\n    api_user: process.env.NMS_API_USER ?? 'admin',\n    api_pass: process.env.NMS_API_PASS ?? 's3cr3tP@ssw0rd'\n  },\n  auth: {\n    play: false,\n    publish: false,\n    secret: process.env.NMS_JWT_SECRET ?? 'superSecretKey!HighlySecure!ChangeMe!',\n    jwt: {\n      expiresIn: '1h',\n      algorithm: 'HS256',\n      users: [\n        {\n          username: process.env.NMS_ADMIN_USERNAME ?? 'admin',\n          password: process.env.NMS_ADMIN_PASSWORD ?? 'p@ssw0rdF0rAdm1n' // IMPORTANT: Hash this in production!\n        }\n      ]\n    }\n  },\n  record: {\n    path: recordPath,\n    append_file: true // Append to existing file if stream restarts\n  },\n  static: {\n    router: '/static',\n    root: htmlRoot\n  }\n};\n\nconst nms = new NodeMediaServer(config);\n\nnms.on('postPublish', (id, StreamPath, args) => {\n  console.log(`[NodeEvent on postPublish] Stream started: ${StreamPath}`);\n  // Example: Log stream details or integrate with other services\n});\n\nnms.on('doneConnect', (id, args) => {\n  console.log(`[NodeEvent on doneConnect] Client disconnected: ${id}`);\n});\n\ntry {\n  nms.run();\n  console.log('Node-Media-Server is running.');\n  console.log(`RTMP server listening on port ${config.rtmp.port}`);\n  console.log(`HTTP server with API listening on port ${config.http.port}`);\n  console.log('Access static files at /static');\n  console.log('Publish stream via RTMP, e.g., rtmp://localhost:1935/live/streamkey');\n  console.log('Play stream via HTTP-FLV, e.g., http://localhost:8000/live/streamkey.flv');\n} catch (error) {\n  console.error('Failed to start Node-Media-Server:', error);\n}","lang":"typescript","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}