แรงดันย้อนกลับและการรวมกลุ่มข้อความ
ป้องกันไม่ให้ผู้ผลิตข้อมูลที่รวดเร็วส่งข้อมูลมากเกินกว่าผู้บริโภคที่ช้าจะรับไหว ด้วยสัญญาณแรงดันย้อนกลับและการรวมกลุ่มข้อความผ่าน WebSockets
แรงดันย้อนกลับและการรวมกลุ่มข้อความ เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
The Problem of Speed Mismatch
When a producer sends faster than a consumer or the network can handle, messages pile up in buffers, memory grows, and latency spikes. Managing this is called backpressure.
The Send Buffer
Each WebSocket has an outgoing buffer. If you keep calling send faster than the socket can flush, that buffer grows unbounded.
Measuring Buffer Pressure
The browser exposes bufferedAmount: bytes queued but not yet sent. A rising value signals the consumer side is falling behind.
if (ws.bufferedAmount > 1_000_000) {
// pause sending
}Applying Backpressure
When the buffer is high, stop producing until it drains. Resume when it falls below a low watermark.
function maybeSend(data) {
if (ws.bufferedAmount < HIGH_WATER) ws.send(data);
else pendingFlush();
}Server-Side Backpressure
On Node.js, ws.send can accept a callback, and the underlying stream signals when to slow down. Respect it instead of blindly flooding.
ws.send(data, () => {
// safe to send the next chunk
});Why Batch Messages
Sending thousands of tiny messages is inefficient: each has framing overhead. Batching groups many updates into one frame, cutting overhead dramatically.
Time-Based Batching
Collect messages for a short window, then flush them together on a timer.
let batch = [];
setInterval(() => {
if (batch.length) { ws.send(JSON.stringify(batch)); batch = []; }
}, 50);Size-Based Batching
Alternatively flush when the batch reaches a target size, capping per-message latency.
function add(msg) {
batch.push(msg);
if (batch.length >= 100) flush();
}Coalescing Updates
For state that changes rapidly (like a cursor position), keep only the latest value instead of every intermediate one. This is coalescing, a powerful form of batching.
The Latency Trade-off
Batching trades a little latency for much higher throughput. Tune the window so users do not perceive lag while you still gain efficiency.
Best Practices
Handle flow control well:
- Watch
bufferedAmountto detect pressure - Pause producing above a high watermark
- Batch tiny messages by time or size
- Coalesce rapidly-changing state
Quick Check
Test your flow-control knowledge.
Recap
You learned flow control over WebSockets:
- Backpressure prevents buffer overload
- Monitor
bufferedAmountand pause above a watermark - Batch tiny messages by time or size
- Coalesce fast-changing state
Your realtime channel now stays stable under load.
เรียนรู้ WebSockets & Realtime Systems Programming ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 47
คำถามที่พบบ่อย
บทเรียน “แรงดันย้อนกลับและการรวมกลุ่มข้อความ” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “แรงดันย้อนกลับและการรวมกลุ่มข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “แรงดันย้อนกลับและการรวมกลุ่มข้อความ”
ป้องกันไม่ให้ผู้ผลิตข้อมูลที่รวดเร็วส่งข้อมูลมากเกินกว่าผู้บริโภคที่ช้าจะรับไหว ด้วยสัญญาณแรงดันย้อนกลับและการรวมกลุ่มข้อความผ่าน WebSockets คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “แรงดันย้อนกลับและการรวมกลุ่มข้อความ” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม
ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การใช้งานการส่งข้อความแบบเผยแพร่และสมัครรับ
- คำขอและการตอบกลับผ่าน WebSockets
- การสตรีมแบบสองทิศทางและการควบคุมการไหล
- แรงดันย้อนกลับและการรวมกลุ่มข้อความ