Just think, a favourite person of yours comes live in Instagram and as you are unaware of this you kept your mobile for charge. Whats now if you get to know this by your friend? You will feel so bad that you have missed the chance to see your favourite person live. This is where our scheduled live streaming application comes to rescue. In our application, people who wants to go live can schedule their live and share the unique links of live video streaming along with the time of live to anyone. Once they started live at the scheduled time, the link they shared will get activated and people can view them live. Coming to our instant live module, people can just start their live video and share their links so that their live video streaming will be available through the links they shared.

Our back end team has created this application with lots of effort. We used the technologies nodejs, mongodb. To generate the unique links we have have used base64id module which generates a random id.

var base64id = require('base64id');
 var Id = base64id.generateId();

Our application looks like this:

 

SCHEDULE:

While creating the schedule the user needs to provide an unique id by which they will be recognized and the time when they will start the schedule. Generate link button will generate an unique link to the user which user can share either by copying it or by using the social links given in our application.

The route for adding the schedule to our mongodb database is coded as below:

app.post('/add', function(request, response) {
     Info.inform.find({
        "name": request.body.name
    }, function(err, inf) {
        if (err) {
            return console.log(err);
        }
        if (!inf.length) {
            Info.inform.create(request.body, function(err, inf) {
                if (err) return response.status(400).send(err);
                inf.save(function(err, inf) {
                    if (err) return response.status(400).send(err);
                    console.log("success");
                    response.status(200).send("successfully submitted");
                });
            });
        } else {
            return response.status(400).send("id exists");
        }
    });
});

START A SCHEDULE:

User needs to enter their id to start their schedule, we will check the current time with the scheduled time. If the time matches the user will be allowed to go live otherwise appropriate error will be raised. To know the present time, we are using the moment module.

The route for the starting a schedule is coded as below:

app.post('/Matching', function(request, response) { 
 Info.inform.find({ 
  "name": request.body.name 
 }, function(err, inf) { 
  if (err) { 
   return console.log(err); 
  } 
  if (inf.length) { 
   Info.inform.find({ 
    "name": request.body.name, 
    "hour": moment().format('hh'), 
    "min": { 
     $lte: (moment().format('mm')) 
    }, 
    "meridiem": moment().format('a'), 
    "year": moment().format('YYYY'), 
    "month": moment().format('MM'), 
    "date": moment().format('DD') 
   }, function(err, infos) { 
    if (err) 
     return response.status(400).send("error"); 
    if (infos.length) { 
     for (var i = 0; i < dblink.length; i++) { 
      dblink.splice(dblink[i].link, 1); 
      fs.writeFileSync('./models/dblink.json', JSON.stringify(dblink), function(err) { 
       if (err) console.log(err); 
      }); 
     } 
     console.log("success"); 
     for (var i = 0; i < dblink.length; i++) { 
      if (dblink[i].link == infos[0].toObject().link) { 
       return response.status(301).json({ 
        "err": "name already exists" 
       }); 
       var flag = 1; 
      } 
     } 
     if (flag != 1) { 
      dblink.push({ 
       "link": infos[0].toObject().link 
      }); 
     } 
     response.json(infos[0].toObject().link); 
     var link = infos[0].toObject().link; 
     data = JSON.stringify(dblink); 
     fs.writeFile('./models/dblink.json', data, finished); 
     function finished(err) { 
      console.log('all set.'); 
      reply = { 
       link: link 
      } 
     } 
    } else { 
     response.status(400).send("time doesnt match"); 
    } 
   }).select({ 
    "link": 1, 
    "_id": 0 
   }); 
  } else { 
   response.status(400).send("id doesnot exist") 
  } 
 }); 
});

INSTANT LIVE:

In instant live we have used both the routes ‘add’ and ‘matching’.

Here the user will give some unique id and generate the link. The generated link can be shared to anyone.Internally after submiting, we will consider the present time as a scheduled time and call the add route. Soon after adding the data to database, matching route will be called and after matching the live video streaming will be started.

The basic implementation of video streaming is taken from project webrtc. The code for the user to start the live using the unique link is written in base64 module. If the user id is already stored in the database and if the schedule time matches then the live will start with the respective link generated at the time of schedule registration.

var data = fs.readFileSync(__dirname + '/../../../models/dblink.json');
	var dblink = JSON.parse(data);
	var w = dblink.length;
	for (var i = 0; i < w; i++) {
	    console.log('1');
   	 var f1 = 0;
    	var linkadd = dblink[0].link;
    	dblink.splice(dblink[0].link, 1);
   	 fs.writeFileSync(__dirname + '/../../../models/dblink.json', 	JSON.stringify(dblink), function(err) {
  	      if (err) console.log(err);
  	  });
	}

The Backend code we wrote for the instant and scheduled live streaming supports android,ios and web frontend team.

Happy Live Streaming!!!!

gousia shareef

Author: gousia shareef