title: Laravel Webrtc video chat with react and pusher
body: <p>I created a web app for video using Laravel, pusher and react js but now problem is that I was following the tutorial I don't know about the react but I'm good in Laravel.
  Now I want to add a function where I can send email to a user so he can join me in video chat, right now it is working on click functions where react take an id and send the request to the client channel.
  here is my react code.</p>
  
  <pre><code>import React, { Component } from 'react';
  import ReactDOM from 'react-dom';
  import MediaHandler from '../MediaHandler';
  import Pusher from 'pusher-js';
  import Peer from 'simple-peer';
  
  const APP_KEY = 'removed-app-key';
  
  export default class App extends Component {
      constructor() {
          super();
  
          this.state = {
              hasMedia: false,
              otherUserId: null
          };
  
          this.user = window.user;
          this.user.stream = null;
          this.peers = {};
  
          this.mediaHandler = new MediaHandler();
          this.setupPusher();
  
          this.callTo = this.callTo.bind(this);
          this.setupPusher = this.setupPusher.bind(this);
          this.startPeer = this.startPeer.bind(this);
      }
  
      componentWillMount() {
          this.mediaHandler.getPermissions()
              .then((stream) =&gt; {
                  this.setState({hasMedia: true});
                  this.user.stream = stream;
  
                  try {
                      this.myVideo.srcObject = stream;
                  } catch (e) {
                      this.myVideo.src = URL.createObjectURL(stream);
                  }
  
                  this.myVideo.play();
              })
      }
  
      setupPusher() {
          Pusher.logToConsole=true;
          this.pusher = new Pusher(APP_KEY, {
              authEndpoint: '/pusher/auth',
              cluster: 'ap2',
              auth: {
                  params: this.user.id,
                  headers: {
                      'X-CSRF-Token': window.csrfToken
                  }
              }
          });
  
          this.channel = this.pusher.subscribe('presence-video-channel');
  
          this.channel.bind(`client-signal-${this.user.id}`, (signal) =&gt; {
              let peer = this.peers[signal.userId];
  
              // if peer is not already exists, we got an incoming call
              if(peer === undefined) {
                  this.setState({otherUserId: signal.userId});
                  peer = this.startPeer(signal.userId, false);
              }
  
              peer.signal(signal.data);
          });
      }
  
      startPeer(userId, initiator = true) {
          const peer = new Peer({
              initiator,
              stream: this.user.stream,
              trickle: false
          });
  
          peer.on('signal', (data) =&gt; {
              this.channel.trigger(`client-signal-${userId}`, {
                  type: 'signal',
                  userId: this.user.id,
                  userName:this.user.name,
                  data: data
              });
          });
  
          peer.on('stream', (stream) =&gt; {
              try {
                  this.userVideo.srcObject = stream;
              } catch (e) {
                  this.userVideo.src = URL.createObjectURL(stream);
              }
  
              this.userVideo.play();
          });
  
          peer.on('close', () =&gt; {
              let peer = this.peers[userId];
              if(peer !== undefined) {
                  peer.destroy();
              }
  
              this.peers[userId] = undefined;
          });
  
          return peer;
      }
  
      callTo(userId) {
          this.peers[userId] = this.startPeer(userId);
      }
  
      render() {
          return (
              &lt;div className="App"&gt;
                  {[1,2,3,4].map((userId) =&gt; {
                      return this.user.id !== userId ? &lt;button key={userId} onClick={() =&gt; this.callTo(userId)}&gt;Call {name}&lt;/button&gt; : null;
                  })}
  
                  &lt;div className="video-container"&gt;
                      &lt;video className="my-video" ref={(ref) =&gt; {this.myVideo = ref;}}&gt;&lt;/video&gt;
                      &lt;video className="user-video" ref={(ref) =&gt; {this.userVideo = ref;}}&gt;&lt;/video&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          );
      }
  }
  
  if (document.getElementById('app')) {
      ReactDOM.render(&lt;App /&gt;, document.getElementById('app'));
  }
  </code></pre>
  
  <p>Here is my pusher function.</p>
  
  <pre><code>public function authenticate(Request $request){
          $socketId= $request-&gt;socket_id;
          $channelName= $request-&gt;channel_name;
          $pusher = new Pusher('APP_KEY', 'APP_SECRET','APP_ID',['cluster'=&gt; 'ap2','forceTLS'=&gt;true]);
          $presence_data = ['name' =&gt; auth()-&gt;user()-&gt;name];
          $key = $pusher-&gt;presence_auth($channelName, $socketId, auth()-&gt;id(), $presence_data);
  
          return response($key);
      }
  </code></pre>
  
  <p>My head script</p>
  
  <pre><code>!-- Scripts --&gt;
      @if(auth()-&gt;user())
          &lt;script&gt;
              window.user = {
                  id:{{auth()-&gt;id()}},
                  name:"{{auth()-&gt;user()-&gt;first_name}}"
              };
              window.csrfToken = "{{ csrf_token() }}";
          &lt;/script&gt;
      @endif
  </code></pre>
  
  <p>The simple thing I want to create a room where I can send email to user to join me one-to-one video chat.
  I'm searching this from last night but no good result till now</p>