Hazelcast Node.js Client
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
-
Error: Could not connect to any of the cluster members
cause The client could not establish a connection to any specified Hazelcast cluster member. This often indicates no Hazelcast server is running at the configured address/port, or network firewall issues.fixVerify that a Hazelcast cluster member is running and accessible at the `clusterMembers` addresses specified in your `ClientConfig`. Common default is `127.0.0.1:5701`. Check firewalls if connecting to a remote server. -
TypeError: client.getSqlService is not a function
cause This error occurs when using a Hazelcast Node.js Client v5.0.0 or newer with code that still calls `getSqlService()`.fixRename `client.getSqlService()` to `client.getSql()` as the method was renamed in version 5.0.0. -
TypeError: The 'await' operator can only be used in an async function
cause You are using `await` outside of an `async` function context. The Hazelcast client API is promise-based and requires `await` calls to be within `async` functions.fixWrap your client initialization and operations in an `async` function, then call that function. For top-level awaiting in modules, ensure your Node.js version supports it and your `package.json` is configured for ESM.
Warnings
- breaking The compact serialization's Rabin fingerprint calculation was fixed in v5.6.0. While typically not causing issues, it's advised to verify production environments before upgrading to ensure data compatibility if compact serialization is heavily used.
- breaking The method `Client.getSqlService()` was renamed to `Client.getSql()` in version 5.0.0. Older code using the deprecated method will fail.
- breaking For SQL compatibility with `LocalDate`, `LocalDateTime`, and `OffsetDateTime` types, the year field is now sent as an integer. This requires using a Hazelcast Node.js client v5.0+ with a Hazelcast server v5.0+.
- gotcha Hazelcast Node.js Client v5.0.0 was deprecated on npm shortly after release due to an npm-related mistake. Users should avoid installing v5.0.0 directly.
- gotcha Compact Serialization, introduced as BETA in v5.1.0 and stabilized in v5.2.0, requires a compatible Hazelcast server version (v5.2 or newer) for stable operation. Using an older server version with a client utilizing stable compact serialization may lead to issues.
Install
-
npm install hazelcast-client -
yarn add hazelcast-client -
pnpm add hazelcast-client
Imports
- Client
const { Client } = require('hazelcast-client');import { Client } from 'hazelcast-client'; - ClientConfig
import { ClientConfig } from 'hazelcast-client/lib/core';import { Client, ClientConfig } from 'hazelcast-client'; - IMap
import { Map } from 'hazelcast-client';import { IMap } from 'hazelcast-client';
Quickstart
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);