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

การสำรวจแบบยาวและวิวัฒนาการสู่การสตรีม

ทำความเข้าใจการสำรวจแบบยาวในฐานะเทคนิคเชื่อมระหว่าง HTTP แบบดั้งเดิมกับการขนส่งข้อมูลสดสมัยใหม่ เรียนรู้ว่ายังมีความสำคัญเมื่อใด และเปรียบเทียบกับ WebSockets และ SSE

บทเรียน 4 จาก 413 ขั้นตอน

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

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

Why Long Polling Exists

Before WebSockets and SSE were widely supported, developers needed a way to push data to clients over plain HTTP. Long polling filled that gap.

With long polling, the client makes a request and the server holds it open until new data is available, instead of replying immediately.

Short Polling vs Long Polling

Short polling hammers the server on a fixed interval, wasting requests when nothing changed.

  • Short polling: request, immediate empty reply, wait, repeat.
  • Long polling: request, server waits, replies only when data arrives.

Long polling cuts the volume of empty responses dramatically.

The Long Polling Cycle

The lifecycle is a loop:

  • Client sends a request.
  • Server holds it open (often with a timeout).
  • When an event occurs, the server responds.
  • Client processes data and immediately reconnects.

A Simple Client Loop

A long polling client recursively re-issues the request after each response. This keeps a near-continuous channel open.

async function poll() {
  try {
    const res = await fetch('/api/updates');
    const data = await res.json();
    handle(data);
  } catch (e) {
    console.error(e);
  }
  poll();
}
poll();

Server Side: Holding the Request

On the server, you avoid replying until an event fires or a timeout is reached. This often uses an event emitter or a pending-promise registry.

app.get('/api/updates', (req, res) => {
  const onEvent = (data) => {
    res.json(data);
    emitter.off('update', onEvent);
  };
  emitter.on('update', onEvent);
  setTimeout(() => {
    emitter.off('update', onEvent);
    res.status(204).end();
  }, 30000);
});

Timeouts Matter

Never hold a request forever. Proxies, load balancers, and mobile networks will silently drop idle connections.

Use a server-side timeout (for example 30s) that returns an empty 204, prompting the client to reconnect cleanly.

Handling Reconnection Gaps

Between a response and the next request there is a tiny window where events could be missed. Use a cursor or last-seen ID so the server can replay anything that happened during the gap.

GET /api/updates?since=10427

Long Polling vs WebSockets

  • WebSockets: one persistent, bidirectional connection. Lowest latency.
  • Long polling: repeated HTTP requests. Higher overhead but works everywhere HTTP works.

WebSockets win for chat and games; long polling wins for hostile network environments and legacy proxies.

Long Polling vs SSE

SSE keeps one connection open and streams many events down it. Long polling reopens a connection per event.

SSE is generally more efficient for unidirectional push, but long polling has broader compatibility and simpler proxy behavior.

Where Long Polling Still Wins

  • Corporate networks that block WebSocket upgrades.
  • Old proxies that buffer streaming responses.
  • Serverless platforms with short execution limits, used as a fallback.

Many libraries (Socket.IO included) fall back to long polling automatically.

Scaling Considerations

Each held request consumes a server slot. With thousands of clients you need non-blocking I/O (Node, Go, async Python) so held requests do not exhaust threads.

Sticky sessions or a shared pub/sub layer (Redis) let multiple servers coordinate events.

Quick Check

Test your understanding of long polling.

Recap

Long polling holds an HTTP request open until data is ready, then the client reconnects. It bridges classic HTTP and true streaming.

  • More efficient than short polling.
  • Less efficient than WebSockets/SSE, but more compatible.
  • Use timeouts and a cursor to stay reliable.
เริ่มต้นได้ฟรี

เรียนรู้ Real-Time Streaming Systems (WebRTC + Live Data) ด้วย AI tutor — ฟรี

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

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

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

บทเรียน “การสำรวจแบบยาวและวิวัฒนาการสู่การสตรีม” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การสำรวจแบบยาวและวิวัฒนาการสู่การสตรีม”

ทำความเข้าใจการสำรวจแบบยาวในฐานะเทคนิคเชื่อมระหว่าง HTTP แบบดั้งเดิมกับการขนส่งข้อมูลสดสมัยใหม่ เรียนรู้ว่ายังมีความสำคัญเมื่อใด และเปรียบเทียบกับ WebSockets และ SSE คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสำรวจแบบยาวและวิวัฒนาการสู่การสตรีม” ใช้เวลานานแค่ไหน

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

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

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

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

  1. ข้อมูลสดเทียบกับ HTTP แบบดั้งเดิม
  2. WebSockets สำหรับการรับส่งข้อมูลสองทิศทาง
  3. Server-Sent Events (SSE) สำหรับการส่งข้อมูลทางเดียว
  4. การสำรวจแบบยาวและวิวัฒนาการสู่การสตรีม
← กลับไปที่ Real-Time Streaming Systems (WebRTC + Live Data)