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"
vael-redis / vael-messaging (opt-in in vaeljs-core, disabled by default; absent in vaeljs-lite-core)Example
Section titled “Example”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();}RedisConnectOptionsvjs onlyv0.0.0-alpha.2connect(options).RedisSetOptionsvjs onlyv0.0.0-alpha.2RedisClient.set().RedisSubscriptionvjs onlyv0.0.0-alpha.2subscribe().RedisClientvjs onlyv0.0.0-alpha.2connect().Functions
Section titled “Functions”connect
Section titled “connect”export function connect(options: RedisConnectOptions): Promise<RedisClient>;
Connect asynchronously to a Redis server (vael-redis feature required).
| Name | Type | Description |
|---|---|---|
options | RedisConnectOptions | Redis connection configuration. |
Promise<RedisClient> — Promise resolving to a connected RedisClient.TypeError— on invalid argument types.RedisError— on connection, authentication, TLS, or timeout failures.
Classes
Section titled “Classes”RedisError
Section titled “RedisError”export class RedisError extends Error {
override name: "RedisError";
code: RedisErrorCode;
constructor(code: RedisErrorCode, message: string);
}
Redacted Redis client error carrying a stable RedisErrorCode.
constructor(code: RedisErrorCode, message: string);
Construct a RedisError.
| Name | Type | Description |
|---|---|---|
code | RedisErrorCode | Machine-readable RedisErrorCode. |
message | string | Human-readable redacted error description. |
| Property | Type | Description | Since |
|---|---|---|---|
name | "RedisError" | Error name ("RedisError"). | v0.0.0-alpha.2 |
code | RedisErrorCode | Machine-readable Redis error code. | v0.0.0-alpha.2 |
Interfaces
Section titled “Interfaces”RedisConnectOptions
Section titled “RedisConnectOptions”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).
| Property | Type | Description | Since |
|---|---|---|---|
host | string | Server hostname or IP address (required). | v0.0.0-alpha.2 |
port optional | number | Server TCP port (1..=65535, default 6379). | v0.0.0-alpha.2 |
username optional | string | Optional ACL username for AUTH / HELLO. | v0.0.0-alpha.2 |
password optional | string | Optional password for AUTH / HELLO. | v0.0.0-alpha.2 |
db optional | number | Logical database index (0..=65535, default 0). | v0.0.0-alpha.2 |
tls optional | boolean | Enable fail-closed rustls TLS transport (default false). | v0.0.0-alpha.2 |
protocol optional | 2 | 3 | RESP protocol version (2 or 3). | v0.0.0-alpha.2 |
maxConnections optional | number | Maximum pooled connections (1..=128). | v0.0.0-alpha.2 |
connectTimeoutMs optional | number | TCP/TLS connect timeout in milliseconds (1..=300000). | v0.0.0-alpha.2 |
commandTimeoutMs optional | number | Per-command execution timeout in milliseconds (1..=300000). | v0.0.0-alpha.2 |
RedisSetOptions
Section titled “RedisSetOptions”export interface RedisSetOptions {
ex?: number;
px?: number;
nx?: boolean;
xx?: boolean;
keepTtl?: boolean;
get?: boolean;
}
Options accepted by RedisClient.set().
| Property | Type | Description | Since |
|---|---|---|---|
ex optional | number | Expiration time in seconds (EX). | v0.0.0-alpha.2 |
px optional | number | Expiration time in milliseconds (PX). | v0.0.0-alpha.2 |
nx optional | boolean | Only set the key if it does not already exist (NX). | v0.0.0-alpha.2 |
xx optional | boolean | Only set the key if it already exists (XX). | v0.0.0-alpha.2 |
keepTtl optional | boolean | Retain existing time-to-live associated with key (KEEPTTL). | v0.0.0-alpha.2 |
get optional | boolean | Return the previous value stored at key, or null (GET). | v0.0.0-alpha.2 |
RedisSubscription
Section titled “RedisSubscription”export interface RedisSubscription {
unsubscribe(...channels: string[]): Promise<void>;
close(): Promise<void>;
[Symbol.asyncDispose](): Promise<void>;
}
Active Redis Pub/Sub subscription handle returned by subscribe().
RedisSubscription.unsubscribe
Section titled “RedisSubscription.unsubscribe”unsubscribe(...channels: string[]): Promise<void>;
Unsubscribe from specific channels, or close the subscription if no channels are provided.
| Name | Type | Description |
|---|---|---|
...channels | string[] | — |
Promise<void>RedisSubscription.close
Section titled “RedisSubscription.close”close(): Promise<void>;
Close the Pub/Sub subscription and release its dedicated connection.
Promise<void>RedisSubscription.[Symbol.asyncDispose]
Section titled “RedisSubscription.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the subscription asynchronously when disposed via await using.
Promise<void>RedisClient
Section titled “RedisClient”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().
RedisClient.command
Section titled “RedisClient.command”command<T = RedisValue>(cmd: string, ...args: Array<string | number | Uint8Array>): Promise<T>;
Execute an arbitrary Redis command with raw arguments.
| Name | Type | Description |
|---|---|---|
cmd | string | — |
...args | Array<string | number | Uint8Array> | — |
Promise<T>RedisClient.ping
Section titled “RedisClient.ping”ping(message?: string): Promise<string>;
Send PING with an optional message and return the server reply.
| Name | Type | Description |
|---|---|---|
message optional | string | — |
Promise<string>RedisClient.get
Section titled “RedisClient.get”get(key: string): Promise<string | Uint8Array | null>;
Get string/binary value for key, or null if absent.
| Name | Type | Description |
|---|---|---|
key | string | — |
Promise<string | Uint8Array | null>RedisClient.set
Section titled “RedisClient.set”set(
key: string,
value: string | number | Uint8Array,
options?: RedisSetOptions,
): Promise<string | Uint8Array | null>;
Set key to value with optional expiration/condition flags.
| Name | Type | Description |
|---|---|---|
key | string | — |
value | string | number | Uint8Array | — |
options optional | RedisSetOptions | — |
Promise<string | Uint8Array | null>RedisClient.del
Section titled “RedisClient.del”del(...keys: string[]): Promise<number>;
Delete one or more keys and return the number of keys removed.
| Name | Type | Description |
|---|---|---|
...keys | string[] | — |
Promise<number>RedisClient.exists
Section titled “RedisClient.exists”exists(...keys: string[]): Promise<number>;
Count how many of the specified keys exist.
| Name | Type | Description |
|---|---|---|
...keys | string[] | — |
Promise<number>RedisClient.expire
Section titled “RedisClient.expire”expire(key: string, seconds: number): Promise<number | bigint>;
Set a timeout on key in seconds (1 if timeout was set, 0 otherwise).
| Name | Type | Description |
|---|---|---|
key | string | — |
seconds | number | — |
Promise<number | bigint>RedisClient.incr
Section titled “RedisClient.incr”incr(key: string): Promise<number | bigint>;
Atomically increment integer key by 1.
| Name | Type | Description |
|---|---|---|
key | string | — |
Promise<number | bigint>RedisClient.hget
Section titled “RedisClient.hget”hget(key: string, field: string): Promise<string | Uint8Array | null>;
Get field value from hash key, or null if missing.
| Name | Type | Description |
|---|---|---|
key | string | — |
field | string | — |
Promise<string | Uint8Array | null>RedisClient.hset
Section titled “RedisClient.hset”hset(key: string, field: string, value: string | number | Uint8Array): Promise<number>;
Set field in hash key to value, returning number of new fields added.
| Name | Type | Description |
|---|---|---|
key | string | — |
field | string | — |
value | string | number | Uint8Array | — |
Promise<number>RedisClient.hgetall
Section titled “RedisClient.hgetall”hgetall(key: string): Promise<Record<string, string | Uint8Array>>;
Return all fields and values of hash key.
| Name | Type | Description |
|---|---|---|
key | string | — |
Promise<Record<string, string | Uint8Array>>RedisClient.lpush
Section titled “RedisClient.lpush”lpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;
Prepend values to list key, returning resulting list length.
| Name | Type | Description |
|---|---|---|
key | string | — |
...values | Array<string | number | Uint8Array> | — |
Promise<number>RedisClient.rpush
Section titled “RedisClient.rpush”rpush(key: string, ...values: Array<string | number | Uint8Array>): Promise<number>;
Append values to list key, returning resulting list length.
| Name | Type | Description |
|---|---|---|
key | string | — |
...values | Array<string | number | Uint8Array> | — |
Promise<number>RedisClient.lpop
Section titled “RedisClient.lpop”lpop(key: string): Promise<string | Uint8Array | null>;
Remove and return the first element of list key, or null.
| Name | Type | Description |
|---|---|---|
key | string | — |
Promise<string | Uint8Array | null>RedisClient.rpop
Section titled “RedisClient.rpop”rpop(key: string): Promise<string | Uint8Array | null>;
Remove and return the last element of list key, or null.
| Name | Type | Description |
|---|---|---|
key | string | — |
Promise<string | Uint8Array | null>RedisClient.lrange
Section titled “RedisClient.lrange”lrange(key: string, start: number, stop: number): Promise<Array<string | Uint8Array>>;
Return elements [start, stop] from list key.
| Name | Type | Description |
|---|---|---|
key | string | — |
start | number | — |
stop | number | — |
Promise<Array<string | Uint8Array>>RedisClient.publish
Section titled “RedisClient.publish”publish(channel: string, message: string | number | Uint8Array): Promise<number>;
Publish message to channel, returning subscriber delivery count.
| Name | Type | Description |
|---|---|---|
channel | string | — |
message | string | number | Uint8Array | — |
Promise<number>RedisClient.subscribe
Section titled “RedisClient.subscribe”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.
| Name | Type | Description |
|---|---|---|
channels | string | readonly string[] | — |
onMessage | (message: string | Uint8Array, channel: string) => void | Promise<void> | — |
RedisClient.pipeline
Section titled “RedisClient.pipeline”pipeline(
commands: ReadonlyArray<ReadonlyArray<string | number | Uint8Array>>,
): Promise<Array<RedisValue | RedisError>>;
Execute a batch of commands in a single pipeline round-trip.
| Name | Type | Description |
|---|---|---|
commands | ReadonlyArray<ReadonlyArray<string | number | Uint8Array>> | — |
RedisClient.close
Section titled “RedisClient.close”close(): Promise<void>;
Close the Redis client connection pool. Idempotent.
Promise<void>RedisClient.[Symbol.asyncDispose]
Section titled “RedisClient.[Symbol.asyncDispose]”[Symbol.asyncDispose](): Promise<void>;
Close the client asynchronously when disposed via await using.
Promise<void>Type Aliases
Section titled “Type Aliases”RedisErrorCode
Section titled “RedisErrorCode”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.
RedisValue
Section titled “RedisValue”export type RedisValue =
| string
| number
| bigint
| boolean
| Uint8Array
| null
| RedisValue[]
| { [key: string]: RedisValue };
Value decoded from a RESP bulk/simple string or numeric reply.
