Create a NonLocalStorage instance.
API credentials.
Optionalid: stringOptional ID.
Create a NonLocalStorage instance.
API credentials.
Optionaloptions: InstanceOptionsOptional instance options.
Unique instance identifier
Protected ReadonlyttlIndicates if the WebSocket connection is currently established. True if connected, false otherwise. Only relevant/appropriate if having some listeners attached (i.e. nls.on('message')) If not using any WebSocket feature, it will always return false.
OptionalconnectionConnection ID assigned by the server for the WebSocket connection.
StaticretrieveRetrieves an access token for a given project using API credentials.
The unique identifier of the project.
The API key associated with the project.
The API secret associated with the project.
Optionaloptions: { origin?: string }Optional settings for token retrieval.
Optionalorigin?: string(Optional) The browser origin to use if the API key has configured an origin restriction.
Pass this when minting a token from a backend for use in a browser.
Example: { origin: req.headers.origin }
A promise that resolves to the access token as a string.
Sets the access token to be used for authentication.
The access token string to set.
token expiration in milliseconds from now
Registers a handler function to be called when the access token is about to expire.
A callback function that will be invoked before the access token expires.
Removes a previously registered handler for the access token expiring event.
The callback function to remove from the access token expiring handlers list.
Retrieve or initialize encryption settings for end-to-end encryption.
OptionalsaltLength: numberOptional salt length in bytes (default: 16).
Promise resolving to encryption settings information.
Rotate encryption keys to enhance security.
OptionalsaltLength: numberOptional salt length in bytes (default: 16).
Promise resolving to new encryption settings information.
Store a value by key.
The key name.
The value to store.
Optionaloptions: { ttl?: number; ifAbsent?: boolean; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalifAbsent?: booleanIf true only set if item absent.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
Metadata about the stored item.
Store multiple values at once.
Object of key/value pairs. Example:
{
key1: { value: 'foo' },
key2: { value: 'bar', ttl: 3600000 },
key2: { value: 'zip', ifAbsent: true }
}
ttl is time-to-live in milliseconds for each item.
Metadata for each stored item.
Retrieve multiple values by key.
Array of key names.
Object of items found.
Retrieve all items, optionally filtered by prefix.
Optionaloptions: { prefix?: string }Optional prefix filter.
Object of all items.
Get all keys, optionally filtered by prefix.
Optionaloptions: { prefix?: string }Optional prefix filter.
Array of key names.
Remove a value by key.
The key name.
Remove multiple values by key.
Array of key names.
Atomically increment a numeric value.
The key name.
Amount to increment by (default 1).
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item.
Atomically decrement a numeric value.
The key name.
Amount to decrement by (default 1).
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item.
Atomically adds an element to an item that is an array. If the item does not exist, it will be created as a new array.
The key of the array item.
The element to push to the array.
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item metadata.
Atomically modifies an array item using splice semantics.
The key of the array item.
The index to start changing the array.
The number of elements to remove.
Optionalitems: ValueType[]Optional array of elements to add.
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item metadata.
Atomically merges fields from a given object into an existing JSON object. If the item does not exist, it will be created.
The key of the object item.
The object containing fields to merge.
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item metadata.
Atomically sets a value deep within a nested JSON object using a path.
The key of the object item.
A dot notation string (e.g., "user.profile.name") or an array of keys.
The value to set at the specified path.
Optionaloptions: { ttl?: number; updatedAt?: number }Optional overrides.
Optionalttl?: numberTime-to-live in milliseconds for this item.
OptionalupdatedAt?: numberIf provided, the operation will only succeed if the remote item's updatedAt timestamp matches this value.
The updated item metadata.
Remove all items for this instance.
Send a message to the server or other clients.
The message object to send.
Transport and authentication options.
Optionaltransport?: "ws" | "http"Transport method: 'ws' (WebSocket) or 'http'. Defaults to 'ws'.
Optionalauth?: { userIdSignature?: string; identityToken?: string }Optional authentication credentials for user verification.
OptionaluserIdSignature?: stringThe signature for Mode 3 user verification.
OptionalidentityToken?: stringThe JWT token for Mode 2 user verification.
transport: 'ws' (WebSocket), the message is delivered to other clients but not echoed back to the sender. The sender will not receive their own message via on('message').transport: 'http', the message is delivered to all clients including the sender. The sender will receive their own message via on('message').userIdSignature, the userId should also be provided for proper verification.// Message sent via WebSocket (not received by sender)
await instance.send({ type: 'chat', message: 'Hello!' });
// Message sent via HTTP (received by sender)
await instance.send({ type: 'chat', message: 'Hello!' }, { transport: 'http' });
// Send with Mode 2 user authentication
await instance.send(
{ type: 'chat', message: 'Hello!', userId: '1234 },
{
transport: 'ws',
auth: { identityToken: 'jwt-token-here' }
}
);
// Send with Mode 3 user authentication
await instance.send(
{ type: 'chat', message: 'Hello!', userId: '1234 },
{
transport: 'ws',
auth: {
userIdSignature: 'signature123'
}
}
);
Register an event handler for WebSocket connection events.
The 'connect' event name.
Function to call when connected.
Register an event handler for WebSocket disconnection events.
The 'disconnect' event name.
Function to call when disconnected.
Register an event handler for presence join events.
The 'presence:join' event name.
Function to call when a connection joins.
Register an event handler for presence leave events.
The 'presence:leave' event name.
Function to call when a connection leaves.
Register an event handler for incoming messages.
The 'message' event name.
Function to call when a message is received.
Register an event handler for WebSocket errors.
The 'error' event name.
Function to call when an error occurs.
Register an event handler for all setItem events.
The 'setItem' event name.
Function to call when any item is set.
Register an event handler for specific setItem events.
The 'setItem' event name.
The specific item name to listen for.
Function to call when the named item is set.
Register an event handler for all removeItem events.
The 'removeItem' event name.
Function to call when any item is removed.
Register an event handler for specific removeItem events.
The 'removeItem' event name.
The specific item name to listen for.
Function to call when the named item is removed.
Remove an event handler for WebSocket connection events.
The 'connect' event name.
Function to remove.
Remove an event handler for WebSocket disconnection events.
The 'disconnect' event name.
Function to remove.
Remove an event handler for presence join events.
The 'presence:join' event name.
Function to remove.
Remove an event handler for presence leave events.
The 'presence:leave' event name.
Function to remove.
Remove an event handler for incoming messages.
The 'message' event name.
Function to remove.
Remove an event handler for WebSocket errors.
The 'error' event name.
Function to remove.
Remove an event handler for all setItem events.
The 'setItem' event name.
Function to remove.
Remove an event handler for specific setItem events.
The 'setItem' event name.
The specific item name.
Function to remove.
Remove an event handler for all removeItem events.
The 'removeItem' event name.
Function to remove.
Remove an event handler for specific removeItem events.
The 'removeItem' event name.
The specific item name.
Function to remove.
Join the presence channel to announce this connection to others.
Optional data to associate with this connection.
Optionalauth: { userIdSignature?: string; identityToken?: string }Optional object containing authentication credentials.
OptionaluserIdSignature?: stringThe signature for Mode 3 verification.
OptionalidentityToken?: stringThe JWT for Mode 2 verification.
Get a list of all currently connected clients in the presence channel.
Promise resolving to an array of connected clients with their data.
Each connection object includes:
Your own connection always appears first in the list for convenience.
const connections = await instance.getJoinedConnections();
console.log(`${connections.length} users online`);
const myConnection = connections[0]; // Always your connection
const otherUsers = connections.slice(1); // All other users
connections.forEach(conn => {
console.log(`User: ${conn.data.username}, joined: ${new Date(conn.joinedAt)}`);
});
Provides a persistent, optionally encrypted, remote key-value store. Supports atomic operations, TTL, and presence via websockets.
Remarks
Use this class to interact with the Vaultrice API for storing and retrieving data.