-
Notifications
You must be signed in to change notification settings - Fork 5
Subscribing to Queries
Subscribing to a query lets you receive realtime updates to that query. This is a powerful tool for collaborative apps. It's also just as easy as making a regular entity query.
Before we get started, you can follow the PubSub Server Setup page to set up your own Nymph PubSub server.
When you make an entity query, to subscribe to that query, call subscribe
on the promise instead of then
.
You can also receive a count of how many subscribers there are to that query.
To unsubscribe from the query, use the unsubscribe
method on the subscription object returned by subscribe
.
$scope.unsubscribe = function(){ subscription.unsubscribe(); };
You can subscribe to changes to an entity with the `subscribe` method on the Entity object.
<div dir="rtl">Subscribing to an Entity</div>
```js
var subscription = smithEntity.subscribe(function(){
if (!smithEntity.guid) {
if (confirm("Someone deleted Mr. Smith! Do you want to restore him?")) {
smithEntity.save();
}
} else {
alert("Mr. Smith's entity has changed!");
}
$scope.$apply();
}, function(errObj){
alert("Error: "+errObj.textStatus);
}, function(count){
alert("There are now "+count+" users watching Mr. Smith.");
});
$scope.unsubscribe = function(){
subscription.unsubscribe();
};
⚠️ Warning: Subscriptions are powerful, but can also lead to race conditions. If two clients save the same entity at the same time, whichever client's request goes through second will overwrite the changes of the first.
Home | Setup Guide | API Docs | Full Code API Docs
© Copyright 2014-2019 SciActive.com