Node.js Pure JavaScript ZooKeeper Client

1.1.3 · abandoned · verified Tue Apr 21

node-zookeeper-client is a pure JavaScript client for Apache ZooKeeper, designed to mirror the ZooKeeper Java client API while adhering to Node.js conventions. It provides core functionalities like connecting, creating/removing nodes, retrieving/setting data, and managing ACLs. The current stable version is 1.1.3, last published over four years ago, indicating it is no longer actively maintained. This client has been tested against ZooKeeper version 3.4.*. Unlike some alternatives that wrap the C client, this package is entirely JavaScript-based. Its release cadence is effectively non-existent due to its abandoned status, and it primarily supports callback-based asynchronous operations.

Common errors

Warnings

Install

Imports

Quickstart

This example connects to a local ZooKeeper instance, creates an ephemeral node with data, and then attempts to retrieve its data with a watcher, handling potential 'node exists' errors.

const zookeeper = require('node-zookeeper-client');

const client = zookeeper.createClient('localhost:2181');
const path = '/my-test-node'; // Using a fixed path for a runnable example
const data = Buffer.from('Hello ZooKeeper');

client.once('connected', function () {
    console.log('Connected to the ZooKeeper server.');

    client.create(path, data, zookeeper.ACL.OPEN_ACL_UNSAFE, zookeeper.CreateMode.EPHEMERAL, function (error) {
        if (error) {
            if (error.getCode() === zookeeper.Exception.NODE_EXISTS) {
                console.log('Node %s already exists, skipping creation.', path);
            } else {
                console.error('Failed to create node: %s due to: %s.', path, error);
                client.close();
                return;
            }
        } else {
            console.log('Node: %s is successfully created with data: %s.', path, data.toString());
        }

        // Example of getting data
        client.getData(path, function (event) {
            console.log('Watcher triggered for %s: %s', path, event);
        }, function (error, data, stat) {
            if (error) {
                console.error('Failed to get data from %s due to: %s.', path, error);
            } else {
                console.log('Data of %s is: %s (version: %d).', path, data.toString(), stat.version);
            }
            client.close();
        });
    });
});

client.on('error', function (error) {
    console.error('ZooKeeper client encountered an error: %s', error.stack);
});

client.connect();

view raw JSON →