# MessageBus

Version: 0.5.0
Framework commit: d503df2befa756e681fcdc6250e9552dc7388bea
Canonical URL: /docs/0.5.0/api/ferroui-reactiveui/routing/struct-messagebus/

**struct** · [`ferroui-reactiveui`](/docs/0.5.0/api/ferroui-reactiveui/) · v0.5.0

```rust
struct MessageBus
```

A message bus: a simple way for view models and other objects to talk to each other without
knowing each other (`MessageBus`).

Which messages go where is decided by the type of the message and a contract, a string that
tells apart the messages of the same type and that the client chooses. The typed operations
are the methods of [`IMessageBusExt`](/docs/0.5.0/api/ferroui-reactiveui/routing/trait-imessagebusext/):

```
use ferroui_reactive::ObservableExt;
use ferroui_reactiveui::{IMessageBusExt, MessageBus};
use std::cell::RefCell;
use std::rc::Rc;

let bus = MessageBus::new();
let received = Rc::new(RefCell::new(Vec::new()));
let log = received.clone();
let _subscription = bus.listen::<String>().subscribe_next(move |message| log.borrow_mut().push(message));

bus.send_message("Hello".to_owned());
bus.send_message_with_contract("Elsewhere".to_owned(), Some("other"));
assert_eq!(*received.borrow(), ["Hello"]);
```

A channel delivers on the scheduler registered for it, the current-thread scheduler by
default. A channel holds the latest message, which
`listen_include_latest` replays (the default value
of the type before any message).

[View source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.ReactiveUI/routing/message_bus.rs#L50)

## Members

- [`new`](#function-new)
- [`current`](#function-current)
- [`set_current`](#function-set_current)

<a id="function-new"></a>

### new

```rust
fn new() -> Rc<Self>
```

A bus without channels.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.ReactiveUI/routing/message_bus.rs#L68)

<a id="function-current"></a>

### current

```rust
fn current() -> Rc<dyn IMessageBus>
```

The bus of the application (`Current`): a `MessageBus` unless another one was set.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.ReactiveUI/routing/message_bus.rs#L73)

<a id="function-set_current"></a>

### set_current

```rust
fn set_current(bus: Rc<dyn IMessageBus>)
```

Replaces the bus of the application (`Current { set; }`), for tests or another
implementation.

[Source](https://github.com/wieslawsoltes/FerroUI/blob/d503df2befa756e681fcdc6250e9552dc7388bea/src/FerroUI.ReactiveUI/routing/message_bus.rs#L84)

## Trait implementations

- `Send`
- `Sync`
- `Freeze`
- `UnsafeUnpin`
- `Unpin`
- `UnwindSafe`
- `RefUnwindSafe`
- `Borrow<T>`
- `BorrowMut<T>`
- `Into<U>`
- `From<T>`
- `TryInto<U>`
- `TryFrom<U>`
- `Any`
- `Debug`
- [`IEnableLogger`](/docs/0.5.0/api/ferroui-splat/logging/trait-ienablelogger/)
- [`IMessageBus`](/docs/0.5.0/api/ferroui-reactiveui/interfaces/trait-imessagebus/)

## Availability

Extracted for `aarch64-apple-darwin`. Cargo features are listed in the [crate index](/docs/0.5.0/api/).
