การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์
ส่งการอัปเดตฐานข้อมูลแบบสดไปยังไคลเอนต์ที่เชื่อมต่ออยู่ โดยรับฟังเหตุการณ์การเปลี่ยนแปลงและส่งต่อผ่าน WebSockets แบบเรียลไทม์
การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์ เป็นบทเรียน WebSockets & Realtime Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน WebSockets & Realtime Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 3 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
From Polling to Pushing
Instead of clients repeatedly asking the database for new data, you can push changes the moment they happen. The backend listens to the database and relays updates over WebSockets.
Change Data Capture
Change Data Capture (CDC) is the pattern of observing inserts, updates, and deletes as a stream of events instead of querying for differences.
Database Notification Mechanisms
Many databases emit change events:
- PostgreSQL:
LISTEN/NOTIFYor logical replication - MongoDB: change streams
- Firebase/Supabase: built-in realtime
Postgres LISTEN/NOTIFY
Postgres can send a notification on a channel that your backend subscribes to.
await client.query('LISTEN row_changed');Triggering Notifications
A trigger fires pg_notify on data changes, carrying a payload describing the change.
CREATE TRIGGER notify_change
AFTER INSERT ON orders
FOR EACH ROW EXECUTE FUNCTION notify_row();Receiving the Event
Your Node backend handles the notification and forwards it to interested WebSocket clients.
client.on('notification', (msg) => {
broadcast(JSON.parse(msg.payload));
});Filtering Who Gets Updates
Not every client cares about every change. Route updates by topic, user, or room so clients only receive relevant data.
function broadcast(change) {
toRoom('orders:' + change.userId, change);
}Sending Deltas, Not Snapshots
Transmit just what changed (a delta) rather than the whole dataset. Clients apply the delta to their local copy, saving bandwidth.
ws.send(JSON.stringify({ op: 'update', id: 42, fields: { status: 'shipped' } }));Initial State plus Stream
A common pattern: on connect, send a full snapshot, then stream incremental changes. The client stays in sync without re-fetching everything.
Handling Reconnects
After a reconnect a client may have missed changes. Send a fresh snapshot or a cursor of changes since its last known version to resync safely.
Best Practices
Stream changes reliably:
- Use CDC or LISTEN/NOTIFY instead of polling
- Filter updates per client
- Send deltas, not full snapshots
- Resync on reconnect to fill gaps
Quick Check
Test your change-streaming knowledge.
Recap
You streamed live database changes to clients:
- Use CDC / LISTEN-NOTIFY instead of polling
- Relay change events over WebSockets
- Filter per client and send deltas
- Snapshot on connect, resync on reconnect
Your data now stays live end to end.
คำถามที่พบบ่อย
บทเรียน “การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส WebSockets & Realtime Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส WebSockets & Realtime Systems Programming มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์”
ส่งการอัปเดตฐานข้อมูลแบบสดไปยังไคลเอนต์ที่เชื่อมต่ออยู่ โดยรับฟังเหตุการณ์การเปลี่ยนแปลงและส่งต่อผ่าน WebSockets แบบเรียลไทม์ คุณปฏิบัติ WebSockets & Realtime Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน WebSockets & Realtime Systems Programming หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน WebSockets & Realtime Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน
บทเรียน “การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน WebSockets & Realtime Systems Programming นี้ได้ไหม
ได้ บทเรียน WebSockets & Realtime Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- WebSockets กับ API แบบ RESTful
- การเชื่อมต่อกับคิวข้อความ
- การสตรีมการเปลี่ยนแปลงฐานข้อมูลไปยังไคลเอนต์