> ## Documentation Index
> Fetch the complete documentation index at: https://praison.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# PubSub

> Publish-subscribe messaging

Agents can use publish-subscribe messaging to communicate with each other and external systems.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    Agent[Agent] --> PubSub[PubSub]
    PubSub --> Subscribers([Subscribers])

    classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff

    class Agent agent
    class PubSub,Subscribers tool
    classDef agent fill:#8B0000,color:#fff
    classDef tool fill:#189AB4,color:#fff
```

## Quick Start

**1. Simple Usage**

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
import { createPubSub } from 'praisonai';

const pubsub = createPubSub();

// Subscribe to topic
pubsub.subscribe('orders', (event) => {
  console.log('New order:', event.data);
});

// Publish message
pubsub.publish('orders', { id: '123', total: 99.99 });
```

**2. With Configuration**

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
const agent1 = new Agent({
  pubsub,
  onComplete: (result) => {
    pubsub.publish('task.done', result);
  }
});

pubsub.subscribe('task.done', (event) => {
  agent2.chat(`Process: ${event.data}`);
});
```

***

## User Interaction Flow

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant Agent1
    participant PubSub
    participant Agent2
    
    Agent1->>PubSub: publish('task.done', data)
    PubSub->>Agent2: Notify subscriber
    Agent2->>Agent2: Process event
```

***

## Configuration Levels

```typescript theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
// Level 1: Function - Simple create
const pubsub = createPubSub();

// Level 2: Dict - With options
const pubsub = createPubSub({
  maxListeners: 100,
  wildcard: true
});

// Level 3: Instance - Full control
import { EventEmitterPubSub } from 'praisonai';

const pubsub = new EventEmitterPubSub({
  maxListeners: 100,
  wildcard: true,
  delimiter: '.',
  onError: (err) => console.error(err)
});
```

***

## Use Cases

| Pattern | Description |
| - | - |
| Events | Notify when things happen |
| Decoupling | Components don't know each other |
| Broadcasting | Send to multiple listeners |
| Queuing | Process messages asynchronously |

***

## API Reference

> [card] [EventEmitterPubSub](https://praison.ai/docs/sdk/reference/typescript/classes/EventEmitterPubSub.md)
>
> PubSub implementation

***

## Best Practices

> [accordion] Use descriptive topics
>
> `user.created` is clearer than `event1`.

> [accordion] Handle errors
>
> Subscribers should handle their own errors.

> [accordion] Unsubscribe when done
>
> Prevent memory leaks by cleaning up.

***

## Related

> [card] [Events](https://praison.ai/docs/js/events.md)
>
> Event system

> [card] [Teams](https://praison.ai/docs/js/teams.md)
>
> Multi-agent teams


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.