Skip to content

ServerRedis

reference
1 min readUpdated

Kind: Class

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

Part of: Microservices

ServerRedis is the Redis transport server implementation for NestJS microservices. It creates Redis publisher/subscriber clients, subscribes to message patterns, dispatches incoming requests to registered handlers, and publishes responses or events back through Redis.

Extends: Server

Methods

MethodSignatureReturns
listenlisten(callback: (err?: unknown, ...optionalParams: unknown[]) => void)void
startstart(callback: () => void)void
bindEventsbindEvents(subClient: Redis, pubClient: Redis)void
closeclose()void
createRedisClientcreateRedisClient()Redis
getMessageHandlergetMessageHandler(pub: Redis)void
handleMessagehandleMessage(channel: string, buffer: string, pub: Redis, pattern: string)void
getPublishergetPublisher(pub: Redis, pattern: any, id: string, ctx: RedisContext)void
parseMessageparseMessage(content: any)Record<string, any>
getRequestPatterngetRequestPattern(pattern: string)string
getReplyPatterngetReplyPattern(pattern: string)string
registerErrorListenerregisterErrorListener(client: any)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
getClientOptionsgetClientOptions()Partial<RedisOptions['options']>
createRetryStrategycreateRetryStrategy(times: number)`undefined
unwrapunwrap()T
onon(event: EventKey, callback: EventCallback)void

Properties

PropertyType
transportIdTransportId
subClientRedis
pubClientRedis
isManuallyClosedany
wasInitialConnectionSuccessfulany
pendingEventListenersArray<{ event: keyof RedisEvents; callback: RedisEvents[keyof RedisEvents]; }>

Where it refuses work

  • ServerRedis stops the work with Error when !this.pubClient || !this.subClient — “Not initialized. Please call the "listen"/"startAllMicroservices" method before accessing…”.
  • ServerRedis stops the work with an early return when this.isManuallyClosed, in 3 places.
  • ServerRedis stops the work with an early return when isUndefined((packet as IncomingRequest).id).

When something fails

  • ServerRedis 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
  Client[Redis Microservice Client] -->|Publish request/event| Redis[(Redis)]
  Redis -->|Subscribed channel| ServerRedis[ServerRedis]
  ServerRedis -->|Parse message| Handler[Registered Message Handler]
  Handler -->|Response| ServerRedis
  ServerRedis -->|Publish response| Redis
  Redis --> Client

AI Coding Instructions

  • Register message handlers with addHandler() before calling listen() so Redis subscriptions can route incoming patterns correctly.
  • Keep request patterns stable and serializable; getRequestPattern() uses the pattern to determine the Redis subscription channel.
  • Ensure Redis connection options match the deployed Redis instance, including authentication, TLS, retry behavior, and database selection where required.
  • Always call close() during application shutdown to disconnect both publisher and subscriber Redis clients cleanly.
  • Avoid placing long-running synchronous work in handlers; Redis message processing should delegate expensive work to asynchronous services or workers.

Relationships

  • IMPORTS → isUndefined

Was this page helpful?

Download as PDF
ServerRedis — NestJS head-to-head