Use cloud development database to build more vivid small programs

Use cloud development database to build more vivid small programs


Long connection services are widely used in scenarios such as message reminders, instant messaging, push, live barrage, and games. This article will introduce the persistent connection service of cloud development database- real-time data push , and use it to build more vivid small programs.

What is real-time data push?

Through the real-time data push capability of the cloud development database, the applet can monitor database changes in real time, that is, it supports monitoring according to the query statement given by the developer. Whenever the result of the query statement changes, the applet will receive Contains the push of updated content and responds to real-time data changes.

A specific example is shown in the following figure: Client 1 modifies data and meets the monitoring conditions. When the database receives the change, it will push the changed content of Client 1 to other clients.

What capacity support does cloud development provide?

Self-built long-term connection services need to design a complicated system architecture, and with a lot of development work, the development cost is high. When the long-term connection service is developed and put into use, it will inevitably bring additional maintenance work. Maintenance of infrastructure and service security also requires a lot of time and labor costs.

At this point, the developer can carry out the actual development work of the mini program. Access the WeChat authentication system to obtain user login status, use WeChat open interfaces, etc., and implement specific business logic based on persistent connection services.

Specifically, using real-time data push has the following advantages:

  • Zero development

The use of real-time data push does not need to write server-side code to build a complete service, which reduces the development cost and the developer's use threshold, and can focus on the design and development of the core business.

  • No authentication

The use of real-time data push does not require access to the WeChat authentication system. It naturally integrates native WeChat capabilities, seamlessly integrates with WeChat login, and works out of the box, without authentication, to obtain WeChat user information and use WeChat open interfaces. It reduces the developer's work on the development and maintenance of the authentication function.

  • Zero maintenance

The use of real-time data push does not require maintenance of infrastructure, security management and other issues, reducing maintenance costs.

Real-time data push overall architecture

There is a buzzword: "Where are the years of peace, it's just that someone will carry the weight for you". The above mentioned natural advantages of zero development, zero maintenance, and no authentication of real-time data push, so these "years are quiet" Who is behind their ability to "load forward" for them? Let us uncover the mystery of the real-time data push technology architecture.

The request goes through the WebSocket protocol from the applet to the access layer, and then to the security verification layer for permission verification. Security verification provides simple ACL marking, and also supports advanced database security rule syntax to achieve fine-grained access permission control, which can be selected according to the actual business situation.

For the routing layer and monitoring push layer, the following will introduce us from the aspects of availability , reliability and performance :

  • Availability

For a service, its availability is the first concern. The first is the automatic elimination of abnormal nodes to ensure that the service is normally available when some nodes have problems. The real-time data push routing layer shields abnormal nodes through the fuse and automatic elimination mechanism of Tars. The monitoring push layer uses ETCD real-time heartbeat detection, reporting the heartbeat in 2 seconds, and detecting anomalies in 6 seconds, and immediately executes the relevant processing logic to automatically eliminate abnormal nodes. Secondly, the consistent hashing scheme is used to improve performance and ensure availability. Each long polling request has a QueryID, and the corresponding relationship between the request from the routing layer to the monitoring push layer is generated by a consistent hash generation algorithm, so that the monitoring push layer is load balanced. Finally, the same conditions correspond to the same QueryID to improve performance.

  • reliability

From small programs to real-time data push services are long polling and stateless, providing escort for business reliability. The event auto-increment ID can ensure that the push events are received in an orderly manner. In addition, events are cached locally and Redis at two levels to ensure data security. Support the client to be offline for 3 minutes, the event is not lost, and the event can still be received in order after the disconnection and reconnection.

  • high performance

In terms of the performance of the real-time data push service, cloud development has done some optimization work:

(1) Use the tree data structure to store the watch condition as an index to optimize the monitoring condition matching index algorithm.

(2) For the matching of change events, a multi-coroutine concurrent pipeline is adopted: matching events, push events, and cache events.

(3) When multiple events that meet the monitoring conditions are generated in a short period of time, multiple events will be merged and then pushed to the client. The network time-consuming request for multiple push events is reduced, and the performance is significantly improved.

Simple and easy-to-use API and rich application scenarios

Real-time data push provides a convenient API for developers to use. Developers can implement business logic according to actual scenarios, specifying collections, query conditions, sorting methods, and limiting the number of returns. For more detailed instructions, please refer to WeChat Open Document-Cloud Development Real-time Data Push. Document link:

Real-time data push can be applied to many practical business scenarios, such as instant messaging, status synchronization, and team collaboration. Whether it is a small program or a small game, you can use the real-time data push of the cloud database to realize the actual business functions of the relevant scene.

instant messaging

1. Mini Programs: live chat rooms, bullet screens, etc.

2. Mini games: regional server chat, room chat, private message function, dynamic message, etc.

The idiom stealing food game is a classic instant messaging customer case. It uses real-time data push to realize the dynamic message reminder of stealing food.

State synchronization

1. Mini Program: Chat prompt status, friend is typing, friend 1V1 Q&A PK, etc.

2. Mini games: Gomoku, etc.

Real-time data push is also used in the Q&A PK link of the WeChat Reading Daily One-Answer applet, and the countdown, score, result and other status of both parties’ answers are synchronized in the answering game.


1. Share documents online

2. Project management collaboration


The above content, I hope to help developers to better apply real-time data push, and add the icing on the cake for small programs. Finally, welcome to pay attention to the cloud development official account, and push the latest capabilities and high-quality technical articles about cloud development from time to time.

Author: Liu Guowei, Tencent and so the development team background development engineers.

Reference: Use cloud development database to build more vivid small programs-Cloud + Community-Tencent Cloud