The content of this page has been automatically translated by AI. If you encounter any problems while reading, you can view the corresponding content in Chinese.
Help & Documentation>Tencent CloudBase

Real-Time Push

Last updated: 2025-02-12 10:47:19

Cloud Development Database supports listening to update events of data in the collection that meet the query condition.

Establishing a Listener

Use the watch() method to establish listening and return a watcher object to close the listening.
Any changes to the documents that meet the condition will trigger the onChange callback.
Web
WeChat Mini Program
const cloudbase = require("@cloudbase/js-sdk");

const app = cloudbase.init({
env: "xxxx"
});
// 1. Obtain a database reference
var db = app.database();

const watcher = db
.collection("todos")
.where({
// query...
})
.watch({
onChange: function (snapshot) {
console.log("snapshot", snapshot);
},
onError: function (err) {
console.error("the watch closed because of error", err);
}
});

// 1. Obtain a database reference
const db = wx.cloud.database();

const watcher = db
.collection("todos")
.where({
// query...
})
.watch({
onChange: function (snapshot) {
console.log("snapshot", snapshot);
},
onError: function (err) {
console.error("the watch closed because of error", err);
}
});


Disabling a Listener

Call watcher.close() to close the listening.
Web
WeChat Mini Program
watcher.close();

watcher.close();

Note:
1. Use cases (Real-time push is suitable for broadcast scenarios, with a maximum of 50,000 simultaneous connections within 10 seconds. It is not recommended for high-concurrency unicast scenarios.)
Unicast scenario: The WHERE conditions watched by different users are different.
Broadcast scenario: The WHERE conditions watched by all users are the same.
2. System Limits
2.1 Record count limit for listening The upper limit for the number of records in one listen is 5,000. If this limit is exceeded, an error will be produced and listening will stop. When listening to a large amount of data, initialization will be slower, affecting listening efficiency. If the expected number of records to listen to is less than 5,000 but may exceed 5,000 later, please restart listening before it exceeds 5,000 to ensure it does not exceed this limit.
2.2 Maximum connection limit The maximum connection upper limit is 50,000. If you need a higher connection limit for an event, please contact us at least 30 days in advance.
2.3 Note on collection permission settings The read permission settings of the collection also apply to real-time data push. If the permission is set to allow users to read only their own data, they will not be able to listen to data not created by themselves.