# @solid-primitives/event-bus

> A collection of SolidJS primitives providing various features of a pubsub/event-emitter/event-bus.

Latest version **1.1.4** (published 2026-07-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @solid-primitives/event-bus
pnpm add @solid-primitives/event-bus
yarn add @solid-primitives/event-bus
bun add @solid-primitives/event-bus
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.1.4 |
| Published | 2026-07-04 |
| First published | 2022-01-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 1 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1556 |
| Author | Damian Tarnawski @thetarnav |
| Maintainers | davedbase, lexlohr, thetarnav. |
| Keywords | eventbus, pubsub, event, emitter, solid, primitives |

## Links

- npm: https://www.npmjs.com/package/@solid-primitives/event-bus
- Repository: https://github.com/solidjs-community/solid-primitives
- Homepage: https://primitives.solidjs.community/package/event-bus
- Issues: https://github.com/solidjs-community/solid-primitives/issues
- npm.io page: https://npm.io/package/@solid-primitives/event-bus

## Dependencies (1)

- [@solid-primitives/utils](https://npm.io/package/@solid-primitives/utils.md) ^6.4.1

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 1.1.4 (latest) — 2026-07-04
- 3.0.0-next.3 (next) — 2026-08-12
- 1.0.2-beta.0 (beta) — 2023-03-03
- 3.0.0-next.2 — 2026-07-18
- 3.0.0-next.1 — 2026-07-04
- 3.0.0-next.0 — 2026-06-26
- 1.1.3 — 2026-02-24
- 1.1.2 — 2025-06-29
- 1.1.1 — 2025-04-27
- 1.1.0 — 2025-01-22
- 1.0.11 — 2024-03-05
- 1.0.10 — 2024-02-28
- 1.0.9 — 2024-01-16
- 1.0.8 — 2023-08-04
- 1.0.7 — 2023-05-19
- … 17 more at https://npm.io/package/@solid-primitives/event-bus/versions

## README

<p>
  <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=Event%20Bus" alt="Solid Primitives Event Bus">
</p>

# @solid-primitives/event-bus

[![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/event-bus?style=for-the-badge)](https://bundlephobia.com/package/@solid-primitives/event-bus)
[![size](https://img.shields.io/npm/v/@solid-primitives/event-bus?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/event-bus)
[![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-2.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)

A collection of SolidJS primitives providing various features of a pubsub/event-emitter/event-bus:

- [`createEventBus`](#createeventbus) - Provides a simple way to listen to and emit events.
- [`createEmitter`](#createemitter) - Creates an emitter with which you can listen to and emit various events.
- [`createEventHub`](#createeventhub) - Provides helpers for using a group of buses.
- [`createEventStack`](#createeventstack) - Provides the emitted events as list/history, with tools to manage it.

## Installation

```bash
npm install @solid-primitives/event-bus
# or
pnpm add @solid-primitives/event-bus
# or
yarn add @solid-primitives/event-bus
```

## `createEventBus`

Provides all the base functions of an event-emitter, plus additional functions for managing listeners, it's behavior could be customized with an config object. Good for advanced usage.

### How to use it

```ts
import { createEventBus } from "@solid-primitives/event-bus";

const bus = createEventBus<string>();

// can be used without payload type, if you don't want to send any
createEventBus();

// bus can be destructured:
const { listen, emit, clear } = bus;

const unsub = bus.listen(a => console.log(a));

bus.emit("foo");

// unsub gets called automatically on cleanup
unsub();
```

## `createEmitter`

Creates an emitter with which you can listen to and emit various events.

### How to use it

```ts
import { createEmitter } from "@solid-primitives/event-bus";

const emitter = createEmitter<{
  foo: number;
  bar: string;
}>();
// can be destructured
const { on, emit, clear } = emitter;

emitter.on("foo", e => {});
emitter.on("bar", e => {});

emitter.emit("foo", 0);
emitter.emit("bar", "hello");

// unsub gets called automatically on cleanup
unsub();
```

### `createGlobalEmitter`

Wrapper around `createEmitter`.

Creates an emitter with which you can listen to and emit various events. With this emitter you can also listen to all events.

```ts
import { createGlobalEmitter } from "@solid-primitives/event-bus";

const emitter = createGlobalEmitter<{
  foo: number;
  bar: string;
}>();
// can be destructured
const { on, emit, clear, listen } = emitter;

emitter.on("foo", e => {});
emitter.on("bar", e => {});

emitter.emit("foo", 0);
emitter.emit("bar", "hello");

// global listener - listens to all channels
emitter.listen(e => {
  switch (e.name) {
    case "foo": {
      e.details;
      break;
    }
    case "bar": {
      e.details;
      break;
    }
  }
});
```

## `createEventHub`

Provides helpers for using a group of event buses.

Can be used with `createEventBus`, `createEventStack` or any emitter that has the same api.

### How to use it

#### Creating EventHub

```ts
import { createEventHub } from "@solid-primitives/event-bus";

// by passing an record of Channels
const hub = createEventHub({
  busA: createEventBus(),
  busB: createEventBus<string>(),
  busC: createEventStack<{ text: string }>(),
});

// by passing a function
const hub = createEventHub(bus => ({
  busA: bus<number>(),
  busB: bus<string>(),
  busC: createEventStack<{ text: string }>(),
}));

// hub can be destructured
const { busA, busB, on, emit, listen, value } = hub;
```

#### Listening & Emitting

```ts
const hub = createEventHub({
  busA: createEventBus<void>(),
  busB: createEventBus<string>(),
  busC: createEventStack<{ text: string }>(),
});
// can be destructured
const { busA, busB, on, listen, emit } = hub;

hub.on("busA", e => {});
hub.on("busB", e => {});

hub.emit("busA", 0);
hub.emit("busB", "foo");

// global listener - listens to all channels
hub.listen(e => {
  switch (e.name) {
    case "busA": {
      e.details;
      break;
    }
    case "busB": {
      e.details;
      break;
    }
  }
});
```

#### Accessing values

If a emitter returns an accessor value, it will be available in a `.value` store.

```ts
hub.value.myBus;
// same as
hub.myBus.value();
```

## `createEventStack`

Extends [`createEmitter`](#createemitter). Provides the emitted events in a list/history form, with tools to manage it.

### How to use it

```ts
import { createEventStack } from "@solid-primitives/event-bus";

const bus = createEventStack<string, { message: string }>({
  // toValue parsing function is optional
  toValue: e => ({ message: e })
});
// can be destructured:
const { listen, emit, clear, value } = bus;

const listener: EventStackListener<{ text: string }> = ({ event, stack, remove }) => {
  console.log(event, stack);
  // you can remove the value from stack
  remove();
};
bus.listen(listener);

bus.emit({ text: "foo" });

// a signal accessor:
bus.value() // => { text: string }[]

bus.setValue(stack => stack.filter(item => {...}))
```

## EventBus Utils

### `toPromise`

Turns a stream-like listen function, into a promise resolving when the first event is captured.

```ts
import { toPromise } from "@solid-primitives/event-bus";

const emitter = createEmitter<string>();
const event = await toPromise(emitter.listen);

// can be used together with raceTimeout from @solid-primitives/utils
import { raceTimeout } from "@solid-primitives/utils";
try {
  const event = await raceTimeout(toPromise(emitter.listen), 2000, true, "event was too slow");
  // if event is quicker:
  event; // => string
} catch (err) {
  // if timeouts:
  console.log(err); // => "event was too slow"
}
```

### `once`

Listen to any EventBus/Emitter, but the listener will automatically unsubscribe on the first captured event. So the callback will run only **once**.

```ts
import { once } from "@solid-primitives/event-bus";

const { listen, emit } = createEmitter<string>();
const unsub = once(listen, event => console.log(event));

emit("foo"); // will log "foo" and unsub

emit("bar"); // won't log
```

### `toEffect`

Wraps `emit` calls inside a `createEffect`. It causes that listeners execute having an reactive owner available. It allows for usage of effects, memos and other primitives inside listeners, without having to create a synthetic root.

```ts
import { toEffect } from "@solid-primitives/event-bus";

const { listen, emit } = createEmitter();
const emitInEffect = toEffect(emit);

// owner is needed for creating computations like createEffect
listen(() => console.log(getOwner()));

// ...sometime later (after root initiation):
emit(); // listener will log `null`
emitInEffect(); // listener will log an owner object
```

### `batchEmits`

Wraps `emit` calls inside a `batch` call. It causes that listeners execute in a single batch, so they are not executed in sepatate queue ticks.

```ts
import { createEventBus, batchEmits } from "@solid-primitives/event-bus";

const bus = batchEmits(createEventBus());

const [a, setA] = createSignal(0);
const [b, setB] = createSignal(0);

bus.listen(setA);
bus.listen(setB);

bus.emit(1); // will set both a and b to 1 in a single batch
```

## Demo

https://codesandbox.io/s/solid-primitives-event-bus-6fp4h?file=/index.tsx

## Changelog

See [CHANGELOG.md](./CHANGELOG.md)

---
_Source: https://npm.io/package/@solid-primitives/event-bus · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
