0Pricing
Real-Time Streaming Systems (WebRTC + Live Data) · บทเรียน

การซิงโครไนซ์ข้อมูลเมตาของ WebRTC

เรียนรู้กลยุทธ์การใช้ช่องข้อมูลสด เช่น WebSockets เพื่อซิงโครไนซ์ข้อมูลเมตาที่ไม่เกี่ยวกับสื่อไปพร้อมกับการโทร WebRTC

การซิงโครไนซ์ข้อมูลเมตาของ WebRTC เป็นบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Real-Time Streaming Systems (WebRTC + Live Data) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Real-Time Streaming Systems (WebRTC + Live Data) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What is WebRTC Metadata?

When you think of a WebRTC call, you likely picture audio and video. But what about all the other information that makes a call useful? This is where metadata comes in.

Metadata is "data about data." In WebRTC, it's all the non-media information that enhances your real-time experience, like user names, statuses, or shared application states.

Why Metadata Sync Matters

Synchronizing metadata is crucial for creating rich, interactive real-time applications. It allows you to:

  • Show who's currently speaking
  • Display user names and avatars
  • Share call status (e.g., "on hold", "recording")
  • Enable real-time collaboration features

Without it, a call is just raw audio/video with little context.

WebRTC Core: Media, Not Metadata

WebRTC is excellent at establishing direct peer-to-peer connections for audio, video, and even raw data (via RTCDataChannel). However, its primary signaling mechanism (for SDP/ICE exchange) is designed for connection setup, not continuous metadata exchange.

For broader application state or centralized updates beyond the initial handshake, we need a different approach.

A Dedicated Channel for Live Data

To handle metadata efficiently and flexibly, we often use a separate live data channel that runs alongside our WebRTC connections. This channel typically connects clients to a central server.

A popular choice for this is WebSockets, offering a persistent, bidirectional connection between client and server.

WebSockets + WebRTC: A Powerful Duo

Think of it this way:

  • WebRTC: Handles the direct, peer-to-peer flow of audio, video, and specific data streams between participants.
  • WebSockets: Manages the centralized, client-to-server-to-client exchange of metadata, like user status, shared pointers, or participant lists.

They work together to build a complete real-time experience.

Client-Side WebSocket Connection

On the client-side (your browser), connecting to a WebSocket server is straightforward using JavaScript. You create a new WebSocket object and define event handlers for when the connection opens, receives messages, or closes.

Try running this example in a browser's developer console:

<!DOCTYPE html>
<html>
<head>
  <title>WebSocket Connect</title>
</head>
<body>
  <h1>WebSocket Client</h1>
  <p>Check console for messages.</p>
  <script>
    // Using a public echo server for demo purposes
    const socket = new WebSocket('wss://echo.websocket.events');

    socket.onopen = (event) => {
      console.log('WebSocket connected!', event);
      socket.send('Hello from CoddyKit!');
    };

    socket.onmessage = (event) => {
      console.log('Message from server:', event.data);
    };

    socket.onclose = (event) => {
      console.log('WebSocket disconnected:', event);
    };

    socket.onerror = (error) => {
      console.error('WebSocket error:', error);
    };
  </script>
</body>
</html>

Sending Metadata via WebSocket

Once connected, sending metadata is as simple as calling the send() method on your WebSocket object. It's common practice to send data as JSON strings to easily structure your metadata.

This allows you to update other clients about your status, actions, or any relevant information.

<!DOCTYPE html>
<html>
<head>
  <title>Send Metadata</title>
</head>
<body>
  <h1>Send Metadata Example</h1>
  <p>Check console for sent messages.</p>
  <script>
    const socket = new WebSocket('wss://echo.websocket.events');

    socket.onopen = () => {
      console.log('WebSocket connected!');
      
      // Example: Sending a user status update
      const statusUpdate = {
        type: 'userStatus',
        userId: 'alice123',
        status: 'online',
        timestamp: new Date().toISOString()
      };
      socket.send(JSON.stringify(statusUpdate));
      console.log('Sent status update:', statusUpdate);

      // Example: Sending a "typing" indicator
      const typingIndicator = {
        type: 'typing',
        userId: 'alice123',
        isTyping: true
      };
      socket.send(JSON.stringify(typingIndicator));
      console.log('Sent typing indicator:', typingIndicator);
    };

    socket.onmessage = (event) => {
      console.log('Received (echoed) message:', event.data);
    };

    socket.onerror = (error) => console.error('WebSocket error:', error);
    socket.onclose = () => console.log('WebSocket disconnected.');
  </script>
</body>
</html>

Receiving and Handling Metadata

To receive metadata, you listen to the onmessage event. The received event.data will contain the message from the server. If you sent JSON, remember to parse it back into a JavaScript object.

Your application can then react to these updates, like showing a new participant in a list or displaying a "typing..." indicator.

<!DOCTYPE html>
<html>
<head>
  <title>Receive Metadata</title>
</head>
<body>
  <h1>Receive Metadata Example</h1>
  <p>Open console. Messages will appear when simulated.</p>
  <script>
    // This example uses a mock message source to simulate incoming data.
    // In a real app, 'socket' would be connected to an actual WebSocket server.
    const mockSocket = {
      onmessage: null,
      simulateMessage: function(data) {
        if (this.onmessage) {
          console.log("Simulating message:", data);
          this.onmessage({ data: JSON.stringify(data) });
        }
      }
    };

    // This is how your app would typically handle messages from a real WebSocket
    mockSocket.onmessage = (event) => {
      const receivedData = JSON.parse(event.data);
      console.log('Received metadata:', receivedData);

      if (receivedData.type === 'userStatus') {
        console.log(`User ${receivedData.userId} is now ${receivedData.status}.`);
      } else if (receivedData.type === 'typing') {
        console.log(`${receivedData.userId} is ${receivedData.isTyping ? 'typing...' : 'not typing.'}`);
      }
      // You would update your UI here based on 'receivedData'
    };

    // Simulate some incoming messages after a delay
    setTimeout(() => {
      mockSocket.simulateMessage({ type: 'userStatus', userId: 'bob456', status: 'online' });
    }, 1000);
    setTimeout(() => {
      mockSocket.simulateMessage({ type: 'typing', userId: 'bob456', isTyping: true });
    }, 2000);
    setTimeout(() => {
      mockSocket.simulateMessage({ type: 'typing', userId: 'bob456', isTyping: false });
    }, 3000);
  </script>
</body>
</html>

Example: Synchronizing Participant Lists

Imagine a video call with multiple participants. When someone joins or leaves, how do all other clients know?

A WebSocket server can manage the central list of active participants. When a user connects or disconnects (e.g., via their WebRTC signaling or a dedicated WebSocket message), the server updates its list and then broadcasts the new list to all active clients via their WebSocket connections.

Check Your Understanding

Which of the following is the primary role of a WebSocket connection when synchronizing metadata alongside a WebRTC call?

Recap: Metadata Sync with WebSockets

In this lesson, we explored the critical role of metadata synchronization in WebRTC applications. We learned that while WebRTC excels at media, a separate live data channel, often implemented with WebSockets, is ideal for managing non-media related information like user statuses or participant lists.

This combination creates richer, more interactive real-time experiences. Next, we'll dive deeper into building real-time chat features using WebRTC Data Channels!

คำถามที่พบบ่อย

บทเรียน “การซิงโครไนซ์ข้อมูลเมตาของ WebRTC” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การซิงโครไนซ์ข้อมูลเมตาของ WebRTC” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Real-Time Streaming Systems (WebRTC + Live Data) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Real-Time Streaming Systems (WebRTC + Live Data) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การซิงโครไนซ์ข้อมูลเมตาของ WebRTC”

เรียนรู้กลยุทธ์การใช้ช่องข้อมูลสด เช่น WebSockets เพื่อซิงโครไนซ์ข้อมูลเมตาที่ไม่เกี่ยวกับสื่อไปพร้อมกับการโทร WebRTC คุณปฏิบัติ Real-Time Streaming Systems (WebRTC + Live Data) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Real-Time Streaming Systems (WebRTC + Live Data) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Real-Time Streaming Systems (WebRTC + Live Data) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “การซิงโครไนซ์ข้อมูลเมตาของ WebRTC” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Real-Time Streaming Systems (WebRTC + Live Data) นี้ได้ไหม

ได้ บทเรียน Real-Time Streaming Systems (WebRTC + Live Data) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การซิงโครไนซ์ข้อมูลเมตาของ WebRTC
  2. แชตแบบเรียลไทม์ผ่านช่องข้อมูล
  3. การแชร์สถานะแอปพลิเคชันแบบสด
  4. การถ่ายโอนไฟล์ผ่านช่องทางข้อมูล WebRTC
← กลับไปที่ Real-Time Streaming Systems (WebRTC + Live Data)