1

I tried to implement WebRTC simple-peer video chat application using .net core MVC. But when writing javascript implementation that needs to make peer connection, they have used require("xxx") function and I am getting an error that required is not defined. I know this needs node or requirejs. I have tried installing the node also but it is not working.

 1)let Peer = require('simple-peer');
   let p = new Peer()

can anyone tell me how to load the required function in .netcore MVC project javascript files? (I have also use SignalR)

Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
  • It will be better if you post the code that isn't working. Also, the question isn't clear, you are getting an error while declaring the variable or while is being instantiated? I'm sure if you write your post more as a question-like, and more specific, someone will be able to help. – celerno Apr 05 '20 at 08:16
  • I am getting the error from require function " require is not define". I need to know how can this method can be use in .NET MVC JavaScript. In articles I have saw require is a node.js client side module. if I need to use I need to bring node client side modules.So I need to know how can I bring this modules in asp.netcore MVC project to use require function – janitha kalshan Apr 05 '20 at 10:14

2 Answers2

0

is the module in present in your package.json file ?

if not try:

npm i simple-peer --save-dev

(also check if actually you are getting the name of the node module correctly)

Your Code Should Look Like This

 let Peer = require('simple-peer');

 var peer = new Peer({
    initiator: true,
    trickle: false
})
  • yse I have loaded the simple peer client side library. the issue is in required() function is not define. I need to know how to use require function in .NET standers java script file.(this mean how can I install node client side moduls in .NET core MVC project). – janitha kalshan Apr 05 '20 at 10:19
0
 var stream;

 navigator.mediaDevices.getUserMedia({ video: true, audio: false }, function (stream) {
        stream = stream;
 });

function InitPeer(type) {
                var peer = new SimplePeer({
                    initiator: (type == 'init') ? true : false,
                    stream: stream,
                    trickle: false
                });

So I had the issue to create the simple peer object in using standard java script if we use node.js we would be able to create the simple peer object by using reqired()

var Peer = require('simple-peer');

but I can't use this in standard java script so if I create like this

var peer = new SimplePeer({
                        initiator: (type == 'init') ? true : false,
                        stream: stream,
                        trickle: false
                    });

stream will give an error telling that stream is not recognize. So to avoid that need to create another variable and assign the streaming inside

 navigator.mediaDevices.getUserMedia({ video: true, audio: false }, function (stream) {
            stream = stream;
     });

So now you can use. This part is important if you are going to implement video chat as we need streaming