Hazelcast Node.js Client

5.6.0 · active · verified Wed Apr 22

The Hazelcast Node.js client, currently at version 5.6.0, provides a robust, promise-based API for connecting Node.js applications to a Hazelcast cluster, a distributed computation and storage platform. It enables developers to interact with distributed data structures like Maps, Queues, and Topics, facilitating real-time stream processing and in-memory data grid functionalities. The client supports native JavaScript objects and ships with TypeScript types, ensuring a smooth development experience. Releases are frequent, with significant updates often introducing new features such as stable Compact Serialization in v5.2.0 and SQL JSON support, while maintaining backward compatibility where possible. Its key differentiators include seamless integration with the Hazelcast ecosystem, resilience through data rebalancing and backups across cluster members, and dynamic scalability for handling varying data and computational loads, from edge devices to large cloud deployments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to a local Hazelcast cluster, creating/accessing a distributed map, performing put and get operations, and gracefully shutting down the client. Includes basic error handling and TypeScript types.

import { Client, ClientConfig } from 'hazelcast-client';

async function main() {
    // Basic configuration for connecting to a local Hazelcast cluster
    // Ensure a Hazelcast instance is running, e.g., via `docker run -p 5701:5701 hazelcast/hazelcast`
    const config: ClientConfig = {
        clusterName: 'dev', // Default cluster name for many Hazelcast deployments
        network: {
            clusterMembers: ['127.0.0.1:5701'] // Default address for a local cluster
        }
    };

    let client: Client | undefined;
    try {
        console.log('Attempting to connect to Hazelcast cluster...');
        client = await Client.newHazelcastClient(config);
        console.log('Successfully connected to Hazelcast cluster.');

        // Get or create a distributed map on the cluster
        const map = await client.getMap<string, string>('my-distributed-map');
        console.log(`Accessed distributed map: '${map.getName()}'`);

        const key = 'uniqueKey';
        const value = `Value at ${new Date().toISOString()}`;

        // Put a key-value pair into the distributed map
        await map.put(key, value);
        console.log(`Put '${key}': '${value}' into the map.`);

        // Get the value associated with the given key from the cluster
        const retrievedValue = await map.get(key);
        console.log(`Retrieved value for '${key}': '${retrievedValue}'`);

        // Remove the key from the map
        await map.remove(key);
        console.log(`Removed key '${key}' from the map.`);

    } catch (error) {
        console.error('An error occurred during Hazelcast operations:', error);
    } finally {
        if (client) {
            await client.shutdown();
            console.log('Hazelcast client shutdown successfully.');
        }
    }
}

main().catch(console.error);

view raw JSON →