# RedisContext

**Kind:** Class

**Source:** [`packages/microservices/ctx-host/redis.context.ts`](https://github.com/nestjs/nest/blob/master/packages/microservices/ctx-host/redis.context.ts#L8)

**Part of:** [Microservices](subsystem-packages-microservices)

`RedisContext` provides Redis-specific metadata to microservice message handlers. It wraps the incoming Redis message context and exposes the channel name through `getChannel()`, allowing handlers to identify the source channel.

**Extends:** `BaseRpcContext`

## Methods

| Method | Signature | Returns |
|---|---|---|
| `getChannel` | `getChannel()` | `void` |

## Diagram

```mermaid
graph LR
  Redis[Redis Pub/Sub Channel] --> Server[Redis Transport Server]
  Server --> Context[RedisContext]
  Context --> Handler[Message Handler]
  Handler --> Channel[getChannel()]
```

## Usage

```ts
import { Controller } from '@nestjs/common';
import { Ctx, MessagePattern, RedisContext } from '@nestjs/microservices';

@Controller()
export class NotificationsController {
  @MessagePattern('notifications.*')
  handleNotification(
    payload: { message: string },
    @Ctx() context: RedisContext,
  ) {
    const channel = context.getChannel();

    console.log(`Received "${payload.message}" from ${channel}`);

    return { received: true, channel };
  }
}
```

## AI Coding Instructions

- Use `RedisContext` only in handlers executed through the Redis microservice transport.
- Retrieve the originating Redis channel with `context.getChannel()` rather than relying on a hard-coded channel name.
- Inject the context with `@Ctx()` alongside payload parameters in `@MessagePattern()` handlers.
- Treat the channel value as transport metadata; keep business logic independent of Redis-specific context where possible.

## How it works

`RedisContext` is an exported RPC context class for Redis message handling. It extends `BaseRpcContext` with a one-element argument tuple whose item is a `string`. [redis.context.ts:3-10](packages/microservices/ctx-host/redis.context.ts#L3-L10)

- **Construction:** `new RedisContext(args)` accepts the `[string]` tuple and passes that same tuple to the base-class constructor, which stores it as protected, read-only `args`. [redis.context.ts:3-10](packages/microservices/ctx-host/redis.context.ts#L3-L10) [base-rpc.context.ts:4-5](packages/microservices/ctx-host/base-rpc.context.ts#L4-L5)
- **`getChannel()`** returns the tuple’s first item (`args[0]`); the class documentation identifies this value as the channel name. [redis.context.ts:13-18](packages/microservices/ctx-host/redis.context.ts#L13-L18)
- **Inherited accessors:** `getArgs()` returns the stored tuple, and `getArgByIndex(index)` returns the item at the requested index. [base-rpc.context.ts:7-20](packages/microservices/ctx-host/base-rpc.context.ts#L7-L20)
- **Server construction:** `ServerRedis` creates this context as `new RedisContext([pattern])` while handling an incoming message. Without wildcard mode, it passes the received `channel` as `pattern`; with wildcard mode, it passes the callback’s `pattern` argument. [server-redis.ts:126-142](packages/microservices/server/server-redis.ts#L126-L142) The resulting context is passed to event handling, request handlers, and processing hooks. [server-redis.ts:144-172](packages/microservices/server/server-redis.ts#L144-L172)
- **Validation, errors, and side effects:** Neither the constructor nor `getChannel()` performs runtime validation, throws an error, or mutates external state in the visible code; they store and read the argument tuple. [redis.context.ts:8-18](packages/microservices/ctx-host/redis.context.ts#L8-L18) [base-rpc.context.ts:4-5](packages/microservices/ctx-host/base-rpc.context.ts#L4-L5)
