将数据库变更流式传输给客户端
监听变更事件并通过 WebSockets 实时转发,为已连接的客户端推送实时数据库更新。
将数据库变更流式传输给客户端 是 CoddyKit 上的免费 WebSockets & Realtime Systems Programming 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 WebSockets & Realtime Systems Programming 课程的其余内容,请升级到 CoddyKit PRO。 WebSockets & Realtime Systems Programming 课程共包含 3 节课。
「将数据库变更流式传输给客户端」这节课中我会学到什么?
监听变更事件并通过 WebSockets 实时转发,为已连接的客户端推送实时数据库更新。 你通过在浏览器中直接运行的动手代码来练习 WebSockets & Realtime Systems Programming,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 WebSockets & Realtime Systems Programming 需要有经验吗?
无需任何先前经验。CoddyKit 上的 WebSockets & Realtime Systems Programming 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「将数据库变更流式传输给客户端」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 WebSockets & Realtime Systems Programming 课中编写并运行代码吗?
能。每节 WebSockets & Realtime Systems Programming 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- WebSockets 与 RESTful API
- 连接消息队列
- 将数据库变更流式传输给客户端