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 26, 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

Subscribing to a Query
```js $scope.getTodos = function(archived){ if (subscription) { subscription.unsubscribe(); } subscription = Nymph.getEntities({"class": 'Todo'}, {"type": archived ? '&' : '!&', "tag": 'archived'}).subscribe(function(todos){ $scope.uiState.showArchived = archived; if (todos) { Nymph.updateArray($scope.todos, todos); Nymph.sort($scope.todos, $scope.uiState.sort); } $scope.$apply(); }, null, function(count){ $scope.uiState.userCount = count; $scope.$apply(); }); }; ```
Clone this wiki locally