Skip to content

ClientRedis

reference
2 min readUpdated

Kind: Class

Source: packages/microservices/client/client-redis.ts

Part of: Microservices

ClientRedis is a Redis-backed microservice client that publishes request messages and listens for correlated reply messages. It manages the Redis connection lifecycle—including connection, reconnection, readiness, errors, and shutdown—while applying request and reply channel naming conventions.

Extends: ClientProxy

Methods

MethodSignatureReturns
getRequestPatterngetRequestPattern(pattern: string)string
getReplyPatterngetReplyPattern(pattern: string)string
closeclose()void
connectconnect()Promise<any>
createClientcreateClient()Redis
registerErrorListenerregisterErrorListener(client: Redis)void
registerReconnectListenerregisterReconnectListener(client: { on: (event: string, fn: () => void) => void; })void
registerReadyListenerregisterReadyListener(client: { on: (event: string, fn: () => void) => void; })void
registerEndListenerregisterEndListener(client: { on: (event: string, fn: () => void) => void; })void
handleClosehandleClose()void
getClientOptionsgetClientOptions()Partial<RedisOptions['options']>
onon(event: EventKey, callback: EventCallback)void
unwrapunwrap()T
createRetryStrategycreateRetryStrategy(times: number)`undefined
createResponseCallbackcreateResponseCallback()( channel: string, buffer: string, ) => Promise<void>
publishpublish(partialPacket: ReadPacket, callback: (packet: WritePacket) => any)() => void
dispatchEventdispatchEvent(packet: ReadPacket)Promise<any>
unsubscribeFromChannelunsubscribeFromChannel(channel: string)void

Properties

PropertyType
loggerany
subscriptionsCountany
pubClientRedis
subClientRedis
connectionPromisePromise<any>
isManuallyClosedany
wasInitialConnectionSuccessfulany
pendingEventListenersArray<{ event: keyof RedisEvents; callback: RedisEvents[keyof RedisEvents]; }>

Where it refuses work

  • ClientRedis stops the work with Error when !this.pubClient || !this.subClient — “Not initialized. Please call the "connect" method first.”.
  • ClientRedis stops the work with an early return when this.isManuallyClosed, in 3 places.
  • ClientRedis stops the work with an early return when this.pubClient && this.subClient.
  • ClientRedis stops the work with an early return when isDisposed || err.

When something fails

  • ClientRedis handles failure in 2 places: it logs it and continues in 1, and turns it into a return value in 1.

Diagram

mermaid
graph LR
  App[Application Service] --> ClientRedis[ClientRedis]
  ClientRedis -->|publish: pattern_request| Redis[(Redis)]
  Redis -->|subscribe: pattern_response| ClientRedis
  ClientRedis -->|response/error| App

  ClientRedis --> Connection[Redis Client Connection]
  Connection --> Ready[Ready Listener]
  Connection --> Reconnect[Reconnect Listener]
  Connection --> Errors[Error Listener]
  Connection --> End[End Listener]

AI Coding Instructions

  • Use connect() before sending messages when the client lifecycle is managed manually; reuse a connected client instead of creating one per request.
  • Keep request patterns aligned with the receiving Redis microservice transport configuration; ClientRedis derives separate request and reply channels from the pattern.
  • Use send() for request-response flows and ensure the downstream service returns a response on the corresponding reply channel.
  • Call close() during application shutdown so Redis subscriptions and connections are released cleanly.
  • Do not bypass lifecycle listeners when changing connection behavior; error, reconnect, ready, and end handlers are required for reliable Redis transport operation.

Relationships

  • IMPORTS → Logger
  • IMPORTS → loadPackage

Was this page helpful?

Download as PDF
ClientRedis — NestJS head-to-head