CloudBackendAsync
supports Continuous Queries, a
kind query used in stream processing
, to apply filtering on ever changing data. The continuous query feature in the Mobile Backend is useful to capture updates on
CloudEntity
objects on the backend and instantly share changes with all online users. Developers can use the feature to enable event-driven programming and data binding between UI components and the entities at the backend to realize a live and interactive user experience.
Underlying Implementation
Continuous queries are implemented using a combination of the
Prospective Search API
and
Google Cloud Messaging
.
CloudQuery
’s filtering condition is converted to Prospective Search subscriptions. On the App Engine Admin Console, you can see the status of subscriptions by clicking
Prospective Search
link from the navigation menu. Because all the underlying services used by the continuous query are highly scalable and available, developers have the ability to design a large system that uses thousands of continuous queries online.
Restrictions on Continuous Query
Currently, the Continuous Query on string property supports only equality filters (
F.eq
and
F.ne
).
Although
Prospective Search API
is highly scalable and can support a large number of concurrent subscriptions, App Engine has a quota for the maximum number of subscriptions per application. At time of writing (July 2013), the quota value is 10,000. You can monitor the number of current subscriptions at on the
Quota Details
page of the
App Engine Admin Console
. If you’ve got an application with a large production usage of the API that could exceed that limit, please
contact us
to increase your quota before it hits the limit.
Using Continuous Queries
To use the feature, set
Scope.FUTURE_AND_PAST
or
Scope.FUTURE
on a
CloudQuery
object using the
cloudQuery.setScope()
method.
By default, a
CloudQuery
’s scope is set to
Scope.PAST
, i.e. the query is executed only against the existing data on Datastore. Setting
Scope.FUTURE_AND_PAST
will re-execute the query every time a user who shares the same backend inserts or updates entities that match the query condition. For example:
CloudQuery cq = new CloudQuery("Contact");
cq.setFilter(F.eq("label", "friends"));
cq.setScope(Scope.FUTURE_AND_PAST);
List<CloudEntity> results = cloudBackendAsync.list(cq, handler);
Here, the query is initially executed on existing entities (with the handler receiving the query results). When users insert or update data, all new or updated entities that match the condition
label = "friends"
, are pushed to the client that issued the query and the handler is called.
Setting the scope to
Scope.FUTURE
will result in an empty call to the handler, followed by query results when a matching insert/update is executed.
You can set the duration of continuous query by using
setSubscriptionDuration()
method, passing an integer number of seconds. By default, continuous queries expire within 24 hours of the last explicit invocation by client. To remove existing continuous queries either use the
unsubscribeFromQuery()
method passing the
topicId
, or
clearAllSubscription()
.
You can also delete all continuous queries manually from the Mobile Backend Settings link from the navigation menu of the App Engine Admin Console .