Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Subscribing to Queries

Hunter Perrin edited this page Mar 27, 2015 · 12 revisions

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.

Subscribing to a Query
```js $scope.smiths = []; Nymph.getEntities({"class": 'User'}, {"type": '&', "like": ['name', '% Smith']}).subscribe(function(smiths){ // This function will be called once initially, then every time the // query returns a different result. // The updateArray function will add any newly matching entities, // update any existing entities that have changed, and remove any // entities that no longer match (including deleted entities). Nymph.updateArray($scope.smiths, smiths); $scope.$apply(); }, function(errObj){ alert("Error: "+errObj.textStatus); }); ```

You can also receive a count of how many subscribers there are to that query.

Getting Subscriber Count
```js $scope.smiths = []; Nymph.getEntities({"class": 'User'}, {"type": '&', "like": ['name', '% Smith']}).subscribe(function(smiths){ Nymph.updateArray($scope.smiths, smiths); $scope.$apply(); }, function(errObj){ alert("Error: "+errObj.textStatus); }, function(count){ // This function will be called each time the count of subscribers to this query changes. alert("There are now "+count+" users watching the Smiths."); }); ```

To unsubscribe from the query, use the unsubscribe method on the subscription object returned by subscribe.

Unsubscribing From a Query
```js $scope.smiths = []; var subscription = Nymph.getEntities({"class": 'User'}, {"type": '&', "like": ['name', '% Smith']}).subscribe(function(smiths){ Nymph.updateArray($scope.smiths, smiths); $scope.$apply(); }

$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.

Clone this wiki locally