Core
Guides v2.x
2

# Pub/Sub with Javascript

For this example we will use Node.js. You will need to install Node.js and NPM.

Let's create a new project folder called realtimePubSub:

Copied to clipboard!
    mkdir realtimePubSub

For this code example we need Kuzzle's Javascript SDK. To install it, run:

Copied to clipboard!
    npm install kuzzle-sdk

We can create an index.js file in the realtimePubSub folder to program our test.

Copied to clipboard!
    touch index.js

# Connect to Kuzzle

The first thing we need to do is connect to Kuzzle. To do this write the following code:

Copied to clipboard!
const {
  Kuzzle,
  WebSocket
} = require('kuzzle-sdk');
const kuzzle = new Kuzzle(new WebSocket('kuzzle'));

Replace kuzzle with the IP address or with the name of the Kuzzle server.

# Subscribe to Documents with Specific Criteria

Let's use the subscribe method.

We will perform a subscription request that tells Kuzzle that the App wants to be notified anytime a document is created that contains the message field. We define this subscription filter as follows, for more information about filters click here:

Copied to clipboard!
// Create a filter that defines that the 'message' field exists
const filter = {exists: {field: 'message'}};
// Triggered whenever a document matching the filter is submitted to Kuzzle
const callback = notification => {
  console.log(notification.result._source.message);
};
try {
  await kuzzle.realtime.subscribe(
    'myindex',
    'mycollection',
    filter,
    callback
  );
  console.log('subscribe ok');
} catch (error) {
  console.error(error.message);
}

We have now programmed the subscription side of the test.

# Publish a Document

Now let's move on to the publish side of the test. Here we will publish a document that contains the message field. When Kuzzle receives this message, it will detect that there is a subscriber listening for such messages and will send it to these subscribers, in this case to our Android App.

We will use the publish method that creates a document containing the value hello world in the message field.

Copied to clipboard!
try {
  await kuzzle.realtime.publish(
    'myindex',
    'mycollection',
    {message: 'hello world'}
  );
  console.log('message published');
} catch (error) {
  console.error(error.message);
}

# Run the Test

The full code should look something like this:

Copied to clipboard!
const {
  Kuzzle,
  WebSocket
} = require('kuzzle-sdk');
const kuzzle = new Kuzzle(new WebSocket('kuzzle'));
// Create a filter that defines that the 'message' field exists
const filter = {exists: {field: 'message'}};
// Triggered whenever a document matching the filter is submitted to Kuzzle
const callback = notification => {
  console.log(notification.result._source.message);
};
const run = async () => {
  try {
    await kuzzle.connect();
    /* Create a subscription that triggers a notification
    whenever a user publishes a document with the field 'message' */
    await kuzzle.realtime.subscribe(
      'myindex',
      'mycollection',
      filter,
      callback
    );
    await kuzzle.realtime.publish(
      'myindex',
      'mycollection',
      {message: 'hello world'}
    );
  } catch (error) {
    console.error(error);
  } finally {
    kuzzle.disconnect();
  }
};
run();

Your console should show the following message:

Copied to clipboard!
hello world