SDK
SDK Java v3.x
2

This SDK is deprecated. We recommend to use the Kuzzle SDK-JVM.
A migration guide is available here

Subscribe #

Subscribes by providing a set of filters: messages, document changes and, optionally, user events matching the provided filters will generate real-time notifications, sent to you in real-time by Kuzzle.

Arguments #

public CompletableFuture<String> subscribe(
  final String index, 
  final String collection, 
  final ConcurrentHashMap<String, Object> filters, 
  final NotificationHandler handler, 
  final SubscribeOptions options
) throws NotConnectedException, InternalException
ArgumentTypeDescription
index
String
Index name
collection
String
Collection name
filters
ConcurrentHashMap<String, Object>
ConcurrentHashMap representing a set of filters following Koncorde syntax
handler
NotificationHandler
Handler function to handle notifications
options
SubscribeOptions

(null)
Subscription options

handler #

Handler function that will be called each time a new notification is received. The hanlder will receive a Response as its only argument.

options #

A SubscribeOptions object.

Return #

The room ID.

Usage #

Simple subscription to document notifications

SubscribeOptions options = new SubscribeOptions();
options.setSubscribeToSelf(true);
ConcurrentHashMap<String, Object> filters = new ConcurrentHashMap<>();
filters.put("exists", "name");
ConcurrentHashMap<String, Object> document = new ConcurrentHashMap<>();
document.put("name", "nina-vkote");
kuzzle.getRealtimeController().subscribe(
"nyc-open-data",
"yellow-taxi",
filters,
notification -> {
  if (notification.scope.equals(SubscribeOptions.Scope.IN.toString())) {
    System.out.println("Document entered the scope");
  } else {
    System.out.println("Document left the scope");
  }
}).get();
ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
query.put("controller", "document");
query.put("action", "create");
query.put("index", "nyc-open-data");
query.put("collection", "yellow-taxi");
query.put("_id", "nina-vkote");
query.put("body", document);
kuzzle.query(query).get();

Subscription to document notifications with scope option

SubscribeOptions options = new SubscribeOptions();
options.setSubscribeToSelf(true);
options.setScope(SubscribeOptions.Scope.ALL);
ConcurrentHashMap<String, Object> filters = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> range = new ConcurrentHashMap<>();
ConcurrentHashMap<String, Object> age = new ConcurrentHashMap<>();
age.put("lte", 20);
range.put("age", age);
filters.put("range", range);
kuzzle.getRealtimeController().subscribe(
  "nyc-open-data",
  "yellow-taxi",
  filters,
  notification -> {
    if (notification.scope.equals(SubscribeOptions.Scope.OUT.toString())) {
      System.out.println("Document left the scope");
    } else {
      System.out.println("Document moved in the scope");
    }
  }, options).get();
ConcurrentHashMap document = new ConcurrentHashMap<>();
document.put("age", 19);
ConcurrentHashMap<String, Object> query = new ConcurrentHashMap<>();
query.put("controller", "document");
query.put("action", "create");
query.put("index", "nyc-open-data");
query.put("collection", "yellow-taxi");
query.put("_id", "nina-vkote");
query.put("body", document);
kuzzle.query(query).get();
query.put("action", "update");
document.put("age", 42);
kuzzle.query(query).get();