D
D
dhat2016-12-08 17:21:43
Heroku
dhat, 2016-12-08 17:21:43

How to deploy your WebRTC video chat?

There is working code that only works locally on my PC, uses budo as devDependencies and packages app.js into bundle.js. Simple-peer and socket.io modules are used. I'm trying to deploy an application, for example on Heroku, and it doesn't work.
As far as I understand, it does not work precisely because of the use of budo (which is used in production). Guess I need to replace it with webpack or browserify. So? Or are there other ways to make the app work on a remote server? Here is the code:
app.js

navigator.webkitGetUserMedia({ video: true, audio: true }, function (stream) {


  var Peer = require('simple-peer');
  var peer = new Peer({
    initiator: location.hash === '#init',
    trickle: false,
    stream: stream
  });

  peer.on('signal', function (data) {
    document.getElementById('yourId').value = JSON.stringify(data)
  })

  document.getElementById('connect').addEventListener('click', function () {
    var otherId = JSON.parse(document.getElementById('otherId').value)
    peer.signal(otherId)
  })

  document.getElementById('send'). addEventListener('click', function () {
    var yourMessage = document.getElementById('yourMessage').value
    peer.send(yourMessage)
  })

  peer.on('data', function (data) {
    document.getElementById('messages').textContent += data + '\n'
  })

  peer.on('stream', function (stream) {
    var video = document.createElement('video')
    document.body.appendChild(video)
    video.src = window.URL.createObjectURL(stream)
    video.play()
  })

}, function (err) {
  console.error(err)
})

server.js
var express = require('express');
var app = express();
var nunjucks = require('nunjucks');
var engines = require('consolidate');
// var io = require('socket.io').listen(app);

app.set('port', (process.env.PORT || 5000));

app.use(express.static(__dirname + '/public'));

app.engine('html', engines.nunjucks);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');

io.sockets.on('connection', function (socket) {
  function log() {
    var array = [">>> "];
    for(var i = 0; i < arguments.length; i++) {
      array.push(arguments[i]);
    }
    socket.emit('log', array);
  }

  socket.on('message', function (message) {
    log('Got message: ', message);
    socket.broadcast.emit('message', message); // should be room only
  });

  socket.on('create or join', function (room) {
    var numClients = io.sockets.clients(room).length;

    log('Room ' + room + ' has ' + numClients + ' client(s)');
    log('Request to create or join room', room);

    if(numClients == 0) {
      socket.join(room);
      socket.emit('created', room);
    }

    else if(numClients == 1) {
      io.sockets.in(room).emit('join', room);
      socket.join(room);
      socket.emit('joined', room);
    }

    else { // max two clients
      socket.emit('full', room);
    }

    socket.emit('emit(): client ' + socket.id + ' joined room ' + room);
    socket.broadcast.emit('broadcast(): client ' + socket.id + ' joined room ' + room);
  });
});

app.get('/', function(req, res) {
  res.render('index');
});

app.listen(app.get('port'), function() {
  console.log('Node app is running on port', app.get('port'));
});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question