The problem with RTMP servers

  • Author:Robert Whitney
  • Date:2018/01/14
RTMP is a protocol designed for flash. Unfortunately it can only be played by a flash player, or desktop clients such as VLC media player. With the move from flash to HTML 5 players, it is impossible to get an RTMP stream running. I have been able to overcome this hurdle by creating a server which acts as an RTMP server, then converts the stream over to MP4 and serves it via HTTP. This is just a proof of concept, showing that it is possible to do. I plan on completing this project and putting it on github, however I just don't have the time for it right now, so here is a proof-of-concept which can be modified to suite your needs. Before you can run this you will need to be in the directory containing server.js (this is what I named the file). ## Before you can run the server
$ npm install http rtmpdump rtmp-server
$ sudo apt-get install rtmpdump
## server.js
#!/usr/bin/node
const RtmpServer = require('rtmp-server');
const rtmpServer = new RtmpServer();

rtmpServer.on('error', err => {
  throw err;
});
rtmpServer.on('client', client => {
  client.on('connect', () => {
     console.log('connect', client.app);
  });
  
  client.on('play', ({ streamName }) => {
    console.log('PLAY', streamName);
  });
  
  client.on('publish', ({ streamName }) => {
    console.log('PUBLISH', streamName);
  });
  
  client.on('stop', () => {
    console.log('client disconnected');
  });
});

rtmpServer.listen(1935);

var rtmpdump = require('rtmpdump');
var fs = require('fs');
 
var options = {
  rtmp: 'rtmp://127.0.0.1/live',
  playpath: 'poc',
  v: null // parameter-less command line switches must have null as a value 
};
 
var stream = rtmpdump.createStream(options);
 
stream.on('connected', function(info) {
  // info provides various details about the stream 
  // duration, resolution, codecs, ... 
  console.log(info);
});
 
stream.on('progress', function(kbytes, elapsed, percent) {
  console.log('%s kbytes read, %s secs elapsed, %s%%', kbytes, elapsed, percent);
});
 
stream.on('error', function(err) {
  // as usual, unhandled error events will throw 
  console.log(err);
  process.exit(1);
});
 
stream.on('exit', function(err) {
    console.log(err);
});

var http = require('http');

//create a server object:
http.createServer(function (req, res) {
  stream.pipe(res); //write a response to the client
}).listen(8080); //the server object listens on port 8080
Once setup you can run and start streaming to the server. To stream fire up OBS and put the following information in: * **Host:** rtmp://127.0.0.1/live * **Stream Key:** poc Now point VLC or your HTML5 player of choice to http://127.0.0.1:8080/ and the stream will start to play.
Robert Whitney
I'm a geek, gamer and breaker of things. I'm a programmer by day and an apache attack helicopter by night. Some would call me their spirit animal.
Opinions expressed here, even 💩 ones, are my own and do not represent those of my employer or associates.
Referral Links

Using my referral links is the best way to help me pay for my projects and development servers and get something out of it for yourself.

Copyright©2011 - 2018, Robert Whitney; All rights reserved.