WebSockets & Realtime Systems Programming · บทเรียน

WebTransport และช่องข้อมูล WebRTC

ทำความรู้จัก WebTransport ในฐานะทางเลือกสมัยใหม่ของ WebSockets และความสามารถแบบเพียร์ทูเพียร์ของช่องข้อมูล WebRTC

บทเรียน 1 จาก 411 ขั้นตอน

WebTransport และช่องข้อมูล WebRTC เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

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

Beyond WebSockets: New Horizons

You've mastered WebSockets for realtime communication. But the web evolves, and new challenges require new solutions!

Today, we'll explore two powerful, modern alternatives: WebTransport and WebRTC Data Channels. These protocols offer unique advantages for specific realtime needs.

Introducing WebTransport

WebTransport is a new API that enables sending data between a browser and a server using HTTP/3. Think of it as a next-generation WebSocket, but built on the modern QUIC protocol.

It offers both unreliable datagrams and reliable, ordered streams, giving developers more control over how data is sent.

WebTransport vs. WebSockets

While both provide bidirectional communication, WebTransport offers key advantages:

  • Multiplexing: Multiple independent streams can share one connection, avoiding head-of-line blocking.
  • Unreliable Datagrams: Send small, time-sensitive data without the overhead of guaranteed delivery.
  • Built on QUIC/HTTP/3: Benefits from faster connection setup and better performance over unreliable networks.

WebSockets are simpler but lack these advanced features.

WebTransport: Streams & Datagrams

WebTransport allows you to choose your communication style:

  • Streams: Provide reliable, ordered delivery, similar to TCP. Great for large files or critical messages.
  • Datagrams: Offer unreliable, unordered delivery, similar to UDP. Perfect for low-latency, loss-tolerant data like game state updates or sensor readings.

This flexibility is a major differentiator from WebSockets, which are stream-based only.

WebTransport Client (Conceptual)

Connecting to a WebTransport server in the browser looks similar to WebSockets, but with stream and datagram options. Note: WebTransport is still experimental; browser support may require flags.

async function connectWebTransport() {
  const url = 'https://example.com:4433/wt';
  try {
    const transport = new WebTransport(url);
    await transport.ready;
    console.log('WebTransport connected!');

    // Example: Sending a datagram
    const writer = transport.datagrams.writable.getWriter();
    await writer.write(new Uint8Array([1, 2, 3]));
    console.log('Datagram sent!');

    // Example: Opening a unidirectional stream
    const sendStream = await transport.createUnidirectionalStream();
    const streamWriter = sendStream.getWriter();
    await streamWriter.write(new TextEncoder().encode('Hello Stream!'));
    await streamWriter.close();

  } catch (error) {
    console.error('WebTransport connection failed:', error);
  }
}

// To run this, call connectWebTransport() in a browser console
// with experimental WebTransport enabled and a compatible server.
// connectWebTransport();

WebRTC Data Channels

WebRTC (Web Real-Time Communication) is an open project enabling real-time communication between browsers (peer-to-peer). While famous for audio/video, it also includes Data Channels.

Data Channels allow two browsers to send arbitrary data directly to each other, without needing an intermediary server for every message.

P2P Data: Not Just Media

WebRTC Data Channels provide a secure, efficient way to exchange data peer-to-peer. This is distinct from WebSockets, which always communicate via a server.

Use cases include collaborative editing, file sharing, gaming, and any scenario where direct browser-to-browser communication is beneficial.

Data Channel Features

WebRTC Data Channels offer configurable reliability and ordering:

  • Reliable & Ordered: Guarantees delivery and message order (like TCP).
  • Unreliable & Ordered: Guarantees order but not delivery (useful for quickly changing data).
  • Unreliable & Unordered: No guarantees on delivery or order (like UDP), ideal for very low-latency, loss-tolerant updates.

This flexibility allows fine-tuning for different application requirements.

WebRTC Data Channel Setup (Conceptual)

Setting up a WebRTC Data Channel involves several steps (signaling, ICE negotiation) to establish the peer connection. Once connected, sending data is straightforward.

function setupDataChannel(peerConnection) {
  const dataChannel = peerConnection.createDataChannel('chat');

  dataChannel.onopen = () => {
    console.log('Data Channel is open!');
    dataChannel.send('Hello from my browser!');
  };

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

  dataChannel.onclose = () => {
    console.log('Data Channel closed.');
  };

  dataChannel.onerror = (error) => {
    console.error('Data Channel error:', error);
  };

  // In a real app, peerConnection would be established via signaling
  // and ICE candidates exchanged to connect two browsers.
}

// Example usage after a peerConnection is established:
// const pc = new RTCPeerConnection();
// setupDataChannel(pc);
// (Signaling and ICE would happen here to connect to another peer)

Compare & Contrast

Which statements accurately describe WebTransport or WebRTC Data Channels?

Recap: The Future is Flexible

We explored WebTransport, a modern alternative to WebSockets built on HTTP/3 and QUIC, offering multiplexing, reliable streams, and unreliable datagrams for more control.

We also looked at WebRTC Data Channels, enabling secure, configurable peer-to-peer data exchange directly between browsers, a powerful tool beyond just media streaming.

These technologies provide developers with a richer toolkit for building highly performant and tailored realtime web applications.

เริ่มต้นได้ฟรี

เรียนรู้ WebSockets & Realtime Systems Programming ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
47

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

บทเรียน “WebTransport และช่องข้อมูล WebRTC” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “WebTransport และช่องข้อมูล WebRTC”

ทำความรู้จัก WebTransport ในฐานะทางเลือกสมัยใหม่ของ WebSockets และความสามารถแบบเพียร์ทูเพียร์ของช่องข้อมูล WebRTC คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “WebTransport และช่องข้อมูล WebRTC” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม

ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. WebTransport และช่องข้อมูล WebRTC
  2. ทบทวน Server-Sent Events (SSE)
  3. อนาคตของ API เว็บแบบเรียลไทม์
  4. การประมวลผลที่ขอบเครือข่ายและเรียลไทม์ที่ขอบเครือข่าย
← กลับไปที่ WebSockets & Realtime Systems Programming