> ## 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.

# Event Bus • Rust AI Agent SDK

> EventBus: Event bus for publish-subscribe messaging.

# EventBus

> Defined in the [**bus**](../modules/bus) module.

<Badge color="orange">Rust AI Agent SDK</Badge>

Event bus for publish-subscribe messaging.

## Fields

| Name                   | Type                                    | Description                               |
| ---------------------- | --------------------------------------- | ----------------------------------------- |
| `subscriptions`        | `RwLock&lt;HashMap&lt;EventType`        | Subscriptions by event type               |
| `global_subscriptions` | `RwLock&lt;Vec&lt;Subscription&gt;&gt;` | Global subscriptions (receive all events) |
| `next_id`              | `RwLock&lt;usize&gt;`                   | Next subscription ID                      |
| `history`              | `RwLock&lt;Vec&lt;Event&gt;&gt;`        | Event history (optional)                  |
| `max_history`          | `usize`                                 | Max history size                          |

## Methods

### `new`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn new() -> Self
```

Create a new event bus

### `with_history`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn with_history(max_history: usize) -> Self
```

Create with custom history size

**Parameters:**

| Name          | Type    |
| ------------- | ------- |
| `max_history` | `usize` |

### `unsubscribe`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn unsubscribe(&self, subscription_id: usize) -> bool
```

Unsubscribe by ID

**Parameters:**

| Name              | Type    |
| ----------------- | ------- |
| `subscription_id` | `usize` |

### `publish`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn publish(&self, event: Event) -> ()
```

Publish an event

**Parameters:**

| Name    | Type    |
| ------- | ------- |
| `event` | `Event` |

### `history`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn history(&self) -> Vec<Event>
```

Get event history

### `events_by_type`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn events_by_type(&self, event_type: EventType) -> Vec<Event>
```

Get events by type

**Parameters:**

| Name         | Type        |
| ------------ | ----------- |
| `event_type` | `EventType` |

### `events_by_source`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn events_by_source(&self, source: &str) -> Vec<Event>
```

Get events by source

**Parameters:**

| Name     | Type   |
| -------- | ------ |
| `source` | `&str` |

### `clear_history`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn clear_history(&self) -> ()
```

Clear history

### `subscription_count`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn subscription_count(&self, event_type: EventType) -> usize
```

Get subscription count for an event type

**Parameters:**

| Name         | Type        |
| ------------ | ----------- |
| `event_type` | `EventType` |

### `total_subscriptions`

```rust theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
fn total_subscriptions(&self) -> usize
```

Get total subscription count

## Source

<Card title="View on GitHub" icon="github" href="https://github.com/MervinPraison/PraisonAI/blob/main/src/praisonai-rust/praisonai/src/bus/mod.rs#L201">
  `praisonai/src/bus/mod.rs` at line 201
</Card>
