Skip to content

RedisEvents

reference
1 min readUpdated

Kind: Type

Source: packages/microservices/events/redis.events.ts

Part of: Microservices

Redis events map for the Redis client. Key is the event name and value is the corresponding callback function.

RedisEvents defines the event-to-handler map used by the Redis client integration. Each key is a Redis event name, and its value is the callback function invoked when that event is emitted, providing a typed contract for Redis lifecycle and error handling.

Definition

ts
{ connect: VoidCallback; ready: VoidCallback; error: OnErrorCallback; close: VoidCallback; reconnecting: VoidCallback; end: VoidCallback; warning: OnWarningCallback; }

Diagram

mermaid
graph LR
  RedisClient[Redis Client] -->|emits event| EventName[Redis event name]
  EventName --> RedisEvents[RedisEvents map]
  RedisEvents --> Callback[Registered callback function]
  Callback --> Application[Microservice event handling]

Usage

ts
import type { RedisEvents } from './redis.events';

const redisEvents: RedisEvents = {
  connect: () => {
    console.log('Connecting to Redis...');
  },
  ready: () => {
    console.log('Redis client is ready.');
  },
  error: (error) => {
    console.error('Redis connection error:', error);
  },
  close: () => {
    console.warn('Redis connection closed.');
  },
};

// Register each typed event handler on the Redis client.
for (const [event, handler] of Object.entries(redisEvents)) {
  redisClient.on(event, handler);
}

AI Coding Instructions

  • Keep event names aligned with the Redis client library's supported event names.
  • Define callbacks with parameter types expected by the corresponding Redis event, especially for error handlers.
  • Use RedisEvents when building Redis client configuration or listener-registration utilities.
  • Ensure error callbacks log or report failures without throwing unhandled exceptions.
  • Remove listeners during shutdown or client replacement to prevent duplicate event handling.

Was this page helpful?

Download as PDF
RedisEvents — NestJS head-to-head