SDK
SDK Java v2.x
1

You are currently looking at the documentation of a previous version of Kuzzle. We strongly recommend that you use the latest version. You can also use the version selector in the top menu.

subscribe #

Subscribes to this collection with a set of filters.

The provided callback will be called everytime a notification is received from Kuzzle.


subscribe(filters, [options], callback) #

ArgumentsTypeDescription
filtersJSON ObjectKoncorde Filters
optionsobject(Optional) Subscription configuration. Passed to the Room constructor.
callbackfunctionCallback to call every time a notification is received on this subscription

Options #

OptionTypeDescriptionDefault
volatileJSON ObjectAdditional information passed to notifications to other usersnull
scopestringFilter document notifications depending on their scope status. You may receive entering documents (scope: in), leaving documents (scope: out), all documents changes (scope: all) or filter these notifications completely (scope: none). This filter does not affect pub/sub messages or user events.all
statestringFilter document notifications depending on the state of the modifying request. You may receive real-time notifications when a document is about to be changed (state: pending), or be notified when the change has been fully written in the database (state: done), or both (state: all). This filter does not affect pub/sub messages or user events.done
subscribeToSelfboolean(Don't) subscribe to notifications fired as a consequence of our own queriestrue
usersstringFilter notifications fired upon a user entering the room (user: in), leaving the room (user: out), or both (user: all). Setting this variable to none prevents receiving these notificationsnone

The options object is directly passed to the Room constructor. See the Room object documentation for more information about these options and notifications.


Return Value #

Returns an object exposing the following method: onDone(callback)

The callback argument is called when the subscription ends, either successfully or with an error.

Usage #

JSONObject filter = new JSONObject()
  .put("and", new JSONArray()
    .put(
      new JSONObject().put("in",
        new JSONObject().put("status",
          new JSONArray()
            .put("idle")
            .put("wantToHire")
            .put("toHire")
            .put("riding")
        )
      )
    )
    .put(
      new JSONObject().put("in",
        new JSONObject()
          .put("type", new JSONArray().put("cab"))
      )
    )
    .put(
      new JSONObject().put("geoDistance",
        new JSONObject()
          .put("distance", "10km")
          .put("pos",
            new JSONObject()
              .put("lat", "48.8566140")
              .put("lon", "2.352222")
          )
      )
    )
  );
kuzzle
  .collection("collection", "index")
  .subscribe(filter, new ResponseListener<NotificationResponse>() {
    @Override
    public void onSuccess(NotificationResponse object) {
      // called each time a new notification on this filter is received
      // check the Room/Notifications section of this documentation
      // to get notification examples
    }
    @Override
    public void onError(JSONObject error) {
      // Handle error
    }
  })
  .onDone(new ResponseListener<Room>() {
    @Override
    public void onSuccess(Room response) {
      // Handle subscription success
    }
    @Override
    public void onError(JSONObject error) {
      // Handle subscription error
    }
  });