Enable real-time audio and video communication between local and remote players

by velo_ninja

HTML

<!DOCTYPE html>
<html>
  <body>
    <div id="local-player"></div>
    <div id="remote-player"></div>

    <script src="https://download.agora.io/sdk/release/AgoraRTC_N.js"></script>
    <script>
      const client = AgoraRTC.createClient({ mode: "rtc", codec: "vp8" });

      const APP_ID = "YOUR_AGORA_APP_ID";
      const TOKEN = null;  // or use a generated token for security
      const CHANNEL = "testChannel";

      async function startCall() {
        await client.join(APP_ID, CHANNEL, TOKEN, null);

        const localTrack = await AgoraRTC.createMicrophoneAndCameraTracks();
        localTrack[1].play("local-player");
        await client.publish(localTrack);
      }

      client.on("user-published", async (user, mediaType) => {
        await client.subscribe(user, mediaType);
        if (mediaType === "video") {
          user.videoTrack.play("remote-player");
        }
      });

      startCall();
    </script>
  </body>
</html>