📜 ⬆️ ⬇️

WebRTC - even easier (EasyRTC)

I have not managed to overcome the signal server while writing a multiplayer chat, so I began to look for ready-made frameworks. The easiest of all seemed to me EasyRTC.

According to the instructions, everything is installed very simply, both on Windows and Linux:
1. Download and install Node.js.
2. From Github'a download easyRTC, go to the downloaded folder.
3. We type in the console npm install (recall in the directory of the downloaded easyRTC folder) the necessary packages for Node.js are installed
4. We also start the server in the console: node server.js.
Everything!!! Now, by default, at the time of your server 8080 running examples of using easyRTC are running.
To write your own chat, you need to connect socket.io.js and easyrtc.js on your page, using nginx, you can “proxy” as follows:
In the configuration file:
 upstream backendEasyRTC {
		 server 127.0.0.1:9080;
     }
 In the server section:
 location /socket.io {
                        #proxy_pass http: // 127.0.0.1:9080;
			 proxy_pass http: // backendEasyRTC;
			 proxy_http_version 1.1;
			 proxy_set_header Upgrade $ http_upgrade;
			 proxy_set_header Connection "upgrade";
			 proxy_set_header Host $ host;
         }

Socket.io.js and easyrtc.js (which is located in the api folder) should be placed in static, respectively.

Next, you need to configure easyRTC:

In the easyrtc.js file, change easyRTC.webSocket to:
 easyRTC.webSocket = io.connect ('http: // your server');

and in the config.js configuration file (located in the same place as server.js), change the standard port to yours, in my case 9080:
 config.httpPort = 9080;


Include the necessary files on your page
 <script src = "/ socket.io/socket.io.js"> </ script>
 <script type = "text / javascript" src = "/ static / js / easyrtc.js"> </ script>


We register video tags:
For remote users
 <video autoplay = "autoplay" id = "remoteVideo1"> </ video>
 <video autoplay = "autoplay" id = "remoteVideo2"> </ video>

And to display yourself
 <video autoplay = "autoplay" id = "localVideo" muted = "muted" volume = "0"> </ video>


Run easyRTC at the start of the page:
 var maxCALLERS = 3;  // Number of people in the room
 window.onload - function () {
   easyRTC.setLoggedInListener (callEverybodyElse);  // Fires when a new webrtc stream appears.
   easyRTC.initManaged ("myroom", "localVideo", ["remoteVideo1", "remoteVideo2], loginSuccess);
 // myroom - the name of the room where users will be
 // localVideo - your video
  // ["remoteVideo1", "remoteVideo2] - where to output remote user streams. 
 // loginSuccess - in it you can find your thread id
 }

 function loginSuccess (easyRTCId) {
     console.log ('My id' + easyRTCId);  // Useful for further user identification
 }

 function callEverybodyElse (otherPeople) {
             easyRTC.setLoggedInListener (null); 
             var list = []; 
             var connectCount = 0;
    
             for (var i in otherPeople) { 
                 list.push (i);
             }
     // By default, the new stream is the last in the array
             function establishConnection (position) {
                 function callSuccess () {
                     connectCount ++;
                     if (connectCount <maxCALLERS && position> 0) {
                         establishConnection (position-1);
                     }
                 }
                 function callFailure () {
                     easyRTC.showError ("CALL-REJECTED", "Rejected by other party");
                     if (connectCount <maxCALLERS && position> 0) {
                         establishConnection (position-1);
                     }            
                 }
                 easyRTC.call (list [position], callSuccess, callFailure);    
             }
             if (list.length> 0) { 
                 establishConnection (list.length-1); 
             }
         }


Additionally, you can specify the connection port:
 easyRTC.setSocketUrl (": 9088");

Video bitrate:
 easyRTC.setVideoBandwidth (40);

Video resolution:
 easyRTC.setVideoDims (320,180);  - video resolution

This is the minimum that is needed for multiplayer chat.
Additionally in the easy.c.js config.js files, as well as on Github and Google Groups.
Thank you if you have read to the end.

Website: http://www.easyrtc.com/
Github: https://github.com/priologic/easyrtc
Google Groups: https://groups.google.com/d/forum/easyrtc

Source: https://habr.com/ru/post/436928/