Skip to content
VaelJS Blog

vael:redis

Async Redis client, commands, and Pub/Sub subscriptions (vael:redis).

Available in the vjs (vaeljs) runtime only when built with the opt-in vael-redis (or vael-messaging) Cargo feature; absent in default builds and in vjs-lite.

import ... from "vael:redis"
Since v0.0.0-alpha.2 vjs (V8)
Cargo feature: vael-redis / vael-messaging (opt-in in vaeljs-core, disabled by default; absent in vaeljs-lite-core)
import { connect } from "vael:redis";
const client = await connect({ host: "127.0.0.1", port: 6379 });
try {
await client.set("greeting", "hello", { ex: 60 });
console.log(await client.get("greeting"));
} finally {
await client.close();
}
functionSince v0.0.0-alpha.2vjs only
export function connect(options: RedisConnectOptions): Promise<RedisClient>;

Connect asynchronously to a Redis server (vael-redis feature required).

Parameters
NameTypeDescription
optionsRedisConnectOptionsRedis connection configuration.
Returns
Promise<RedisClient> — Promise resolving to a connected RedisClient.
Exceptions
  • TypeError — on invalid argument types.
  • RedisError — on connection, authentication, TLS, or timeout failures.
classSince v0.0.0-alpha.2vjs only
export class RedisError extends Error {
  override name: "RedisError";
  code: RedisErrorCode;
  constructor(code: RedisErrorCode, message: string);
}

Redacted Redis client error carrying a stable RedisErrorCode.

Constructor
constructor(code: RedisErrorCode, message: string);

Construct a RedisError.

Parameters
NameTypeDescription
codeRedisErrorCodeMachine-readable RedisErrorCode.
messagestringHuman-readable redacted error description.
Properties
PropertyTypeDescriptionSince
name "RedisError"Error name ("RedisError").v0.0.0-alpha.2
code RedisErrorCodeMachine-readable Redis error code.v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface RedisConnectOptions {
  host: string;
  port?: number;
  username?: string;
  password?: string;
  db?: number;
  tls?: boolean;
  protocol?: 2 | 3;
  maxConnections?: number;
  connectTimeoutMs?: number;
  commandTimeoutMs?: number;
}

Connection configuration accepted by connect(options).

Properties
PropertyTypeDescriptionSince
host stringServer hostname or IP address (required).v0.0.0-alpha.2
port optionalnumberServer TCP port (1..=65535, default 6379).v0.0.0-alpha.2
username optionalstringOptional ACL username for AUTH / HELLO.v0.0.0-alpha.2
password optionalstringOptional password for AUTH / HELLO.v0.0.0-alpha.2
db optionalnumberLogical database index (0..=65535, default 0).v0.0.0-alpha.2
tls optionalbooleanEnable fail-closed rustls TLS transport (default false).v0.0.0-alpha.2
protocol optional2 | 3RESP protocol version (2 or 3).v0.0.0-alpha.2
maxConnections optionalnumberMaximum pooled connections (1..=128).v0.0.0-alpha.2
connectTimeoutMs optionalnumberTCP/TLS connect timeout in milliseconds (1..=300000).v0.0.0-alpha.2
commandTimeoutMs optionalnumberPer-command execution timeout in milliseconds (1..=300000).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface RedisSetOptions {
  ex?: number;
  px?: number;
  nx?: boolean;
  xx?: boolean;
  keepTtl?: boolean;
  get?: boolean;
}

Options accepted by RedisClient.set().

Properties
PropertyTypeDescriptionSince
ex optionalnumberExpiration time in seconds (EX).v0.0.0-alpha.2
px optionalnumberExpiration time in milliseconds (PX).v0.0.0-alpha.2
nx optionalbooleanOnly set the key if it does not already exist (NX).v0.0.0-alpha.2
xx optionalbooleanOnly set the key if it already exists (XX).v0.0.0-alpha.2
keepTtl optionalbooleanRetain existing time-to-live associated with key (KEEPTTL).v0.0.0-alpha.2
get optionalbooleanReturn the previous value stored at key, or null (GET).v0.0.0-alpha.2
interfaceSince v0.0.0-alpha.2vjs only
export interface RedisSubscription {
  unsubscribe(...channels: string[]): Promise<void>;
  close(): Promise<void>;
  [Symbol.asyncDispose](): Promise<void>;
}

Active Redis Pub/Sub subscription handle returned by subscribe().

Methods
methodSince v0.0.0-alpha.2
unsubscribe(...channels: string[]): Promise<void>;

Unsubscribe from specific channels, or close the subscription if no channels are provided.

Parameters
NameTypeDescription
...channelsstring[]—
Returns
Promise<void>
methodSince v0.0.0-alpha.2
close(): Promise<void>;

Close the Pub/Sub subscription and release its dedicated connection.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
[Symbol.asyncDispose](): Promise<void>;

Close the subscription asynchronously when disposed via await using.

Returns
Promise<void>
interfaceSince v0.0.0-alpha.2vjs only
export interface RedisClient {
  command<T = RedisValue>(cmd: string, ...args: Array<string | number | Uint8Array>): Promise<T>;
  ping(message?: string): Promise<string>;
  get(key: string): Promise<string | Uint8Array | null>;
  set(
      key: string,
      value: string | number | Uint8Array,
      options?: RedisSetOptions,
    ): Promise<string | Uint8Array | null>;
  del(...keys: string[]): Promise<number>;
  exists(...keys: string[]): Promise<number>;
  expire(key: string, seconds: number): Promise<number | bigint>;
  incr(key: string): Promise<number | bigint>;
  hget(key: string, field: string): Promise<string | Uint8Array | null>;
  hset(key: string, field: string, value: string | number | Uint8Array): Promise<number>;
  hgetall(key: string): Promise<Record<string, string | Uint8Array>>;
  lpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;
  rpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;
  lpop(key: string): Promise<string | Uint8Array | null>;
  rpop(key: string): Promise<string | Uint8Array | null>;
  lrange(key: string, start: number, stop: number): Promise<Array<string | Uint8Array>>;
  publish(channel: string, message: string | number | Uint8Array): Promise<number>;
  subscribe(
      channels: string | readonly string[],
      onMessage: (message: string | Uint8Array, channel: string) => void | Promise<void>,
    ): Promise<RedisSubscription>;
  pipeline(
      commands: ReadonlyArray<ReadonlyArray<string | number | Uint8Array>>,
    ): Promise<Array<RedisValue | RedisError>>;
  close(): Promise<void>;
  [Symbol.asyncDispose](): Promise<void>;
}

Connected async Redis client handle returned by connect().

Methods
methodSince v0.0.0-alpha.2
command<T = RedisValue>(cmd: string, ...args: Array<string | number | Uint8Array>): Promise<T>;

Execute an arbitrary Redis command with raw arguments.

Parameters
NameTypeDescription
cmdstring—
...argsArray<string | number | Uint8Array>—
Returns
methodSince v0.0.0-alpha.2
ping(message?: string): Promise<string>;

Send PING with an optional message and return the server reply.

Parameters
NameTypeDescription
message optionalstring—
Returns
Promise<string>
methodSince v0.0.0-alpha.2
get(key: string): Promise<string | Uint8Array | null>;

Get string/binary value for key, or null if absent.

Parameters
NameTypeDescription
keystring—
Returns
Promise<string | Uint8Array | null>
methodSince v0.0.0-alpha.2
set(
  key: string,
  value: string | number | Uint8Array,
  options?: RedisSetOptions,
): Promise<string | Uint8Array | null>;

Set key to value with optional expiration/condition flags.

Parameters
NameTypeDescription
keystring—
valuestring | number | Uint8Array—
options optionalRedisSetOptions—
Returns
Promise<string | Uint8Array | null>
methodSince v0.0.0-alpha.2
del(...keys: string[]): Promise<number>;

Delete one or more keys and return the number of keys removed.

Parameters
NameTypeDescription
...keysstring[]—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
exists(...keys: string[]): Promise<number>;

Count how many of the specified keys exist.

Parameters
NameTypeDescription
...keysstring[]—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
expire(key: string, seconds: number): Promise<number | bigint>;

Set a timeout on key in seconds (1 if timeout was set, 0 otherwise).

Parameters
NameTypeDescription
keystring—
secondsnumber—
Returns
Promise<number | bigint>
methodSince v0.0.0-alpha.2
incr(key: string): Promise<number | bigint>;

Atomically increment integer key by 1.

Parameters
NameTypeDescription
keystring—
Returns
Promise<number | bigint>
methodSince v0.0.0-alpha.2
hget(key: string, field: string): Promise<string | Uint8Array | null>;

Get field value from hash key, or null if missing.

Parameters
NameTypeDescription
keystring—
fieldstring—
Returns
Promise<string | Uint8Array | null>
methodSince v0.0.0-alpha.2
hset(key: string, field: string, value: string | number | Uint8Array): Promise<number>;

Set field in hash key to value, returning number of new fields added.

Parameters
NameTypeDescription
keystring—
fieldstring—
valuestring | number | Uint8Array—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
hgetall(key: string): Promise<Record<string, string | Uint8Array>>;

Return all fields and values of hash key.

Parameters
NameTypeDescription
keystring—
Returns
Promise<Record<string, string | Uint8Array>>
methodSince v0.0.0-alpha.2
lpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;

Prepend values to list key, returning resulting list length.

Parameters
NameTypeDescription
keystring—
...valuesArray<string | number | Uint8Array>—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
rpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;

Append values to list key, returning resulting list length.

Parameters
NameTypeDescription
keystring—
...valuesArray<string | number | Uint8Array>—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
lpop(key: string): Promise<string | Uint8Array | null>;

Remove and return the first element of list key, or null.

Parameters
NameTypeDescription
keystring—
Returns
Promise<string | Uint8Array | null>
methodSince v0.0.0-alpha.2
rpop(key: string): Promise<string | Uint8Array | null>;

Remove and return the last element of list key, or null.

Parameters
NameTypeDescription
keystring—
Returns
Promise<string | Uint8Array | null>
methodSince v0.0.0-alpha.2
lrange(key: string, start: number, stop: number): Promise<Array<string | Uint8Array>>;

Return elements [start, stop] from list key.

Parameters
NameTypeDescription
keystring—
startnumber—
stopnumber—
Returns
methodSince v0.0.0-alpha.2
publish(channel: string, message: string | number | Uint8Array): Promise<number>;

Publish message to channel, returning subscriber delivery count.

Parameters
NameTypeDescription
channelstring—
messagestring | number | Uint8Array—
Returns
Promise<number>
methodSince v0.0.0-alpha.2
subscribe(
  channels: string | readonly string[],
  onMessage: (message: string | Uint8Array, channel: string) => void | Promise<void>,
): Promise<RedisSubscription>;

Subscribe to one or more Pub/Sub channels with an async/sync message handler.

Parameters
NameTypeDescription
channelsstring | readonly string[]—
onMessage(message: string | Uint8Array, channel: string) => void | Promise<void>—
methodSince v0.0.0-alpha.2
pipeline(
  commands: ReadonlyArray<ReadonlyArray<string | number | Uint8Array>>,
): Promise<Array<RedisValue | RedisError>>;

Execute a batch of commands in a single pipeline round-trip.

Parameters
NameTypeDescription
commandsReadonlyArray<ReadonlyArray<string | number | Uint8Array>>—
methodSince v0.0.0-alpha.2
close(): Promise<void>;

Close the Redis client connection pool. Idempotent.

Returns
Promise<void>
methodSince v0.0.0-alpha.2
[Symbol.asyncDispose](): Promise<void>;

Close the client asynchronously when disposed via await using.

Returns
Promise<void>
typeSince v0.0.0-alpha.2vjs only
export type RedisErrorCode =
  | "VAEL_REDIS_CONFIG"
  | "VAEL_REDIS_CONNECT"
  | "VAEL_REDIS_TLS"
  | "VAEL_REDIS_AUTH"
  | "VAEL_REDIS_TIMEOUT"
  | "VAEL_REDIS_PROTOCOL"
  | "VAEL_REDIS_SERVER"
  | "VAEL_REDIS_LIMIT"
  | "VAEL_REDIS_CLOSED"
  | "VAEL_REDIS_IO"
  | "VAEL_REDIS_NOT_FOUND";

Stable screaming-snake-case error codes reported on RedisError.code.

typeSince v0.0.0-alpha.2vjs only
export type RedisValue =
  | string
  | number
  | bigint
  | boolean
  | Uint8Array
  | null
  | RedisValue[]
  | { [key: string]: RedisValue };

Value decoded from a RESP bulk/simple string or numeric reply.