> For the complete documentation index, see [llms.txt](https://marblejs.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://marblejs.gitbook.io/docs/messaging/microservices/redis-pub-sub.md).

# Redis Pub/Sub

> Aside from data storage, [Redis](https://redis.io/) can be used as a Publisher/Subscriber platform. In this pattern, publishers can issue messages to any number of subscribers on a channel. These messages are fire-and-forget, in that if a message is published and no subscribers exists, the message evaporates and cannot be recovered.

## Installation

Before the usage remember to install required packages.

```bash
$ yarn add redis
```

## Overview

{% tabs %}
{% tab title="microservice.ts" %}

```typescript
import { createMicroservice, Transport } from '@marblejs/messaging';

const microservice = createMicroservice({
  transport: Transport.REDIS
  options: {
    host: 'redis://127.0.0.1:6379',
    channel: 'hello_channel',
  },
  // ...
});
```

{% endtab %}

{% tab title="client.ts" %}

```typescript
import { MessagingClient, createMicroservice, Transport } from '@marblejs/messaging';

const client = MessagingClient({
  transport: Transport.REDIS
  options: {
    host: 'redis://127.0.0.1:6379',
    channel: 'hello_channel',
  },
  // ...
});
```

{% endtab %}
{% endtabs %}

The `options` property is specific to the chosen transport layer. **REDIS** exposes the following properties:

| Attribute  | description                                                     |
| ---------- | --------------------------------------------------------------- |
| `host`     | Host address of the Redis server                                |
| `channel`  | Channel name which your microservice will listen to             |
| `timeout`  | Timeout for RPC-like communication. Defaults to **120 seconds** |
| `port`     | Port of the Redis server                                        |
| `password` | If set, client will run Redis auth command on connect.          |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://marblejs.gitbook.io/docs/messaging/microservices/redis-pub-sub.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
