SDK
SDK Jvm v1.x
2

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.

:::: tabs ::: tab Java

Arguments #

public CompletableFuture<String> subscribe(
  String index, 
  String collection, 
  Map<String, Object> filters,
  String scope,
  String users,
  boolean subscribeToSelf,
  Map<String, Object> volatiles,
  NotificationHandler handler, 
) throws NotConnectedException, InternalException

public CompletableFuture<String> subscribe(
  String index, 
  String collection, 
  Map<String, Object> filters,
  String scope,
  String users,
  boolean subscribeToSelf,
  NotificationHandler handler, 
) throws NotConnectedException, InternalException

public CompletableFuture<String> subscribe(
  String index, 
  String collection, 
  Map<String, Object> filters,
  String scope,
  String users,
  NotificationHandler handler, 
) throws NotConnectedException, InternalException

public CompletableFuture<String> subscribe(
  String index, 
  String collection, 
  Map<String, Object> filters,
  String scope,
  NotificationHandler handler, 
) throws NotConnectedException, InternalException

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

(all)
Subscribes to document entering or leaving the scope
Possible values: all, in, out, none
users
String

(none)
Subscribes to users entering or leaving the room
Possible values: all, in, out, none
subscribeToSelf
boolean

(true)
Subscribes to notifications fired by our own queries
volatile
Map<String, Object>

({})
Map representing subscription information, used in user join/leave notifications

handler #

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

Return #

The room ID.

Usage #

Simple subscription to document notifications_

Map<String, Object> filters = new HashMap<>();
filters.put("exists", "name");
Map<String, Object> document = new HashMap<>();
document.put("name", "nina-vkote");
final String roomId = kuzzle.getRealtimeController().subscribe(
  "nyc-open-data",
  "yellow-taxi",
  filters,
  notification -> {
    if (notification.getScope().equals("in")) {
      System.out.println("Document entered the scope");
    } else {
      System.out.println("Document left the scope");
    }
}).get();
Map<String, Object> query = new HashMap<>();
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_

Map<String, Object> filters = new HashMap<>();
Map<String, Object> range = new HashMap<>();
Map<String, Object> age = new HashMap<>();
age.put("lte", 20);
range.put("age", age);
filters.put("range", range);
kuzzle.getRealtimeController().subscribe(
  "nyc-open-data",
  "yellow-taxi",
  filters,
  "all",
  "all",
  notification -> {
    if (notification.getScope().equals("out")) {
      System.out.println("Document left the scope");
    } else {
      System.out.println("Document moved in the scope");
    }
  }).get();
Map document = new HashMap<>();
document.put("age", 19);
Map<String, Object> query = new HashMap<>();
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();

::: ::: tab Kotlin

Arguments #

fun subscribe(
  index: String?,
  collection: String?,
  filters: Map<String, Any>,
  scope: String = "all",
  users: String = "all",
  subscribeToSelf: Boolean = true,
  volatiles: Map<String?, Any?> = HashMap(),
  handler: (Response) -> Unit): CompletableFuture<String>
ArgumentTypeDescription
index
String
Index name
collection
String
Collection name
filters
Map<String, Object>
Map representing a set of filters following Koncorde syntax
handler
NotificationHandler
Handler function to handle notifications
scope
String

(all)
Subscribes to document entering or leaving the scope
Possible values: all, in, out, none
users
String

(none)
Subscribes to users entering or leaving the room
Possible values: all, in, out, none
subscribeToSelf
boolean

(true)
Subscribes to notifications fired by our own queries
volatile
Map<String, Object>

({})
Map representing subscription information, used in user join/leave notifications

handler #

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

Return #

The room ID.

Usage #

Simple subscription to document notifications_

val filters: Map<String, Any> = HashMap<String, Any>().apply {
  put("exists", "name")
}
val roomId: String = kuzzle.realtimeController.subscribe(
  "nyc-open-data",
  "yellow-taxi",
  filters) {
    if (it.scope == "in") {
      println("Document entered the scope")
    } else {
      println("Document left the scope")
    }
  }.get()
val document: Map<String, Any> = HashMap<String, Any>().apply {
  put("name", "nina-vkote")
}
val query: Map<String?, Any?> = HashMap<String?, Any?>().apply {
  put("controller", "document");
  put("action", "create");
  put("index", "nyc-open-data");
  put("collection", "yellow-taxi");
  put("_id", "nina-vkote");
  put("body", document);
}
kuzzle.query(query).get()

Subscription to document notifications with scope option_

val filters: Map<String, Any> = HashMap<String, Any>().apply {
  put("range", HashMap<String, Any>().apply {
    put("age", HashMap<String, Any>().apply {
      put("lte", 20)
    })
  })
}
val roomId: String = kuzzle.realtimeController.subscribe(
    "nyc-open-data",
    "yellow-taxi",
    filters) {
  if (it.scope == "in") {
    println("Document entered the scope")
  } else {
    println("Document moved in the scope")
  }
}.get()
val document = HashMap<String, Any>().apply {
  put("age", 19)
}
val query = HashMap<String?, Any?>().apply {
  put("controller", "document");
  put("action", "create");
  put("index", "nyc-open-data");
  put("collection", "yellow-taxi");
  put("_id", "nina-vkote");
  put("body", document);
}
kuzzle.query(query).get()
query.put("action", "update")
document.put("age", 42)
kuzzle.query(query).get()

::: ::::