Infinispan Hot Rod JS Client
The `infinispan` package provides an asynchronous, event-driven JavaScript client for Node.js applications to interact with Infinispan data grids using the Hot Rod protocol. Currently at version 0.13.0, it leverages Promises for all asynchronous operations, simplifying error handling and chaining. The client offers comprehensive CRUD capabilities, compare-and-swap operations, expiration management with flexible time units, bulk operations (putAll, getAll, clear), and advanced features like remote cache listeners, server-side script execution, and statistics retrieval. A key differentiator is its robust cluster awareness, supporting consistent hashing for key-based operations, dynamic topology discovery for nodes joining or leaving, and automatic multi-site failover with manual switching capabilities. It's under active development, indicating ongoing feature additions and improvements.
Common errors
-
Error: connect ECONNREFUSED 127.0.0.1:11222
cause The Infinispan server is not running, or the client is trying to connect to the wrong host/port.fixVerify that your Infinispan server is running and listening on the configured host and port (default is 11222). Check firewall rules. Ensure host/port in `infinispan.client()` call are correct. -
TypeError: infinispan.client is not a function
cause The `infinispan` module was imported incorrectly, or the `client` method is being accessed before the module is fully loaded or correctly referenced.fixEnsure you are using `import infinispan from 'infinispan';` for ESM, and then `await infinispan.client(...)`. For CommonJS, use `const infinispan = require('infinispan');`. -
CacheNotFoundException: Cache 'myCache' not found
cause The specified cache name does not exist on the Infinispan server.fixEnsure the cache `myCache` is created and configured on your Infinispan server. You might need to use the server console or management API to create it, or configure the server for automatic cache creation. -
UnhandledPromiseRejectionWarning: Unhandled promise rejection.
cause An asynchronous operation (e.g., `put`, `get`, `connect`) failed, and the returned Promise's rejection was not caught.fixAlways use `.catch()` with Promise chains or `try...catch` blocks with `async/await` to handle potential errors from all client operations.
Warnings
- breaking Older versions of the client (pre-0.6) only supported string keys and values. Native JSON object support was added in version 0.6.0. Using JSON with older clients will result in incorrect serialization.
- breaking For Infinispan Server versions 8.2.x to 9.3.x, use `js-client` version 0.7. Later server versions (9.4.x and above) require `js-client` 0.10.0 or later.
- gotcha The client is under 'heavy development,' implying that API stability between minor versions might not be strictly maintained, and breaking changes could occur in minor or patch releases without explicit major version bumps.
- gotcha Infinispan Server enables authentication by default. Connecting without proper authentication configuration will result in connection failures or access denied errors.
- gotcha To perform queries on caches, the `dataFormat` option must be explicitly set to `'application/x-protostream'`. By default, the client treats key/value pairs as `String`.
Install
-
npm install infinispan -
yarn add infinispan -
pnpm add infinispan
Imports
- infinispan
import { client } from 'infinispan';import infinispan from 'infinispan';
- client
const client = new infinispan.Client(...);
const client = await infinispan.client({ host: '127.0.0.1', port: 11222 }, { cacheName: 'myCache' }); - CacheClient
import type { CacheClient } from 'infinispan';
Quickstart
import infinispan from 'infinispan';
async function runClient() {
let client;
try {
// Connect to Infinispan Server on localhost:11222
// and specify a cache name, e.g., 'myCache'.
// Ensure an Infinispan server is running and 'myCache' exists or is configured for autostart.
client = await infinispan.client(
{ host: process.env.INFINISPAN_HOST ?? '127.0.0.1', port: parseInt(process.env.INFINISPAN_PORT ?? '11222', 10) },
{ cacheName: process.env.INFINISPAN_CACHE_NAME ?? 'myCache' }
);
console.log('Successfully connected to Infinispan!');
const key = 'myKey';
const value = 'myValue';
// Put an entry into the cache
await client.put(key, value);
console.log(`Put: '${key}' -> '${value}'`);
// Get the entry from the cache
const retrievedValue = await client.get(key);
console.log(`Get: '${key}' -> '${retrievedValue}'`);
// Check if the key exists
const contains = await client.containsKey(key);
console.log(`Contains key '${key}': ${contains}`);
} catch (error) {
console.error('Error interacting with Infinispan:', error.message);
} finally {
if (client) {
await client.disconnect();
console.log('Disconnected from Infinispan.');
}
}
}
runClient();