Search code examples
node.jsdigital-oceanrtmpnode-media-server

How to set-up node-media-server with rtmp on DO Droplet


All Files Below

I have trouble hosting my RTMP server on a digital-ocean droplet. I have 2 node application, 1 is just an API written with Hapi.js that runs on port 8000

The second one is a node-media-server app running on port 8888 and 1935 for RTMP, which I have integrated as a Hapi.js plugin, but they run as separate processes. I use Nginx as a reverse proxy to pass requests to node apps, and everything works fine. All the endpoints provided by node-media-server work.

But I can't think of a way to access my node-media-server's 1935 port and send a RTMP stream to it.

On localhost I use OBS like this rtmp://localhost:1935/live/{stream_key}, but the same doesn't work for the hosted app.

Please show me a way to receive the stream from my OBS to server.

Maybe I could use ngix-rtmp module to receive the stream and just push it to my node-media-server app on the server...

/etc/nginx/sites-available/default

upstream media {
   server 127.0.0.1:8888;
}


upstream main {
   server 127.0.0.1:8000;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html/challenger;

    index index.html index.htm index.nginx-debian.html;

    server_name hellonode;

    location ^~ /assets/ {
        gzip_static on;
        expires 12h;
        add_header Cache-Control public;
  }

        location /main/  {
            proxy_pass          http://localhost:8000/;
            #proxy_http_version  1.1;
            #proxy_set_header    Host                $http_host;
            #proxy_set_header    Upgrade             $http_upgrade;
            #proxy_set_header    Connection          "Upgrade";
            #proxy_set_header    X-Real-IP           $proxy_protocol_addr;
            #proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            #proxy_set_header    X-Forwarded-Proto   tcp;
            #proxy_set_header    X-NginX-Proxy       true;
        }

    location / {
        proxy_http_version 1.1;
        proxy_cache_bypass $http_upgrade;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://media;
    }
}

And the node-media-server app (used as a Hapi.js plugin, called in manifest.js

"use strict";

const { getStreamKeyFromStreamPath } = require("../app/services/live.service");

const NodeMediaServer = require("node-media-server");

const ffmpegPath = require("@ffmpeg-installer/ffmpeg").path;

const connect = {
  name: "live",
  pkg: require("../../package"),
  async register(server, options) {
    const config = {
      logType: 3,
      rtmp: {
        port: 1935,
        chunk_size: 60000,
        gop_cache: true,
        ping: 30,
        ping_timeout: 60,
      },
      http: {
        port: process.env.MEDIA_SERVER_PORT || 8888,
        mediaroot: "./media",
        allow_origin: "*",
      },
      trans: {
        //   ffmpeg: "../../../ffmpeg/bin/ffmpeg.exe",
        ffmpeg: ffmpegPath,
        tasks: [
          {
            app: "live",
            hls: true,
            hlsFlags: "[hls_time=2:hls_list_size=3:hls_flags=delete_segments]",
            mp4: true,
            mp4Flags: "[movflags=frag_keyframe+empty_moov]",
          },
        ],
      },
    };

    const nms = new NodeMediaServer(config);

    nms.on("prePublish", async (id, StreamPath, args) => {
      let stream_key = getStreamKeyFromStreamPath(StreamPath);
      console.log(
        "[NodeEvent on prePublish]",
        `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
      );
    });

    nms.run();
  },
};

module.exports = connect;

Solution

  • After some research, I got the solution.

    The solution was painfully obvious

    So node-media-server app listens for RTMP on port 1935. So the natural solution is to create and configure a firewall that will allow TCP connections through port 1935. For Ubuntu 18.0 Droplet the following does the trick.

    First, find Your port with lsof -i :1935, then allow TCP connection over the port with sudo ufw allow 1935/tcp. Thus if the node-media-server is running, congrats! You can now use OBS like this rtmp://your_ip:1935/live/stream_key

    Note that: Watch for the host Your app runs on. For me localhost worked, but with some Droplet configurations You might need to set it to 0.0.0.0