Angular
Angular projects use @busly/angular, which exposes the bus through injectable services and RxJS Observables. Its API deliberately stays close to @ngrx/signals/events, so if you know one, the other will feel familiar. If you use NgRx SignalStore, the integration lives in a separate package, @busly/ngrx.
Installation
pnpm add @busly/angular @busly/core @busly/eventsnpm install @busly/angular @busly/core @busly/eventsbun add @busly/angular @busly/core @busly/eventsIf your project uses NgRx, add @busly/ngrx as well. NgRx's own event creators already have the shape Busly expects, so you can skip @busly/events entirely (see Reusing ngrx event creators):
pnpm add @busly/angular @busly/core @busly/ngrxnpm install @busly/angular @busly/core @busly/ngrxbun add @busly/angular @busly/core @busly/ngrxThe adapter is built for Angular 22 and works without any provider setup.
The examples on this page use the counterPageEvents group from Define events.
Dispatching events in Angular
injectBusDispatch
The quickest way to send events is injectBusDispatch. Much like NgRx's injectDispatch, it gives you one function per event in the group:
import { Component } from '@angular/core';
import { injectBusDispatch } from '@busly/angular';
import { counterPageEvents } from './counter.events';
@Component({
selector: 'app-counter-controls',
template: `
<button (click)="counter.increment()">+1</button>
<button (click)="counter.set(0)">Reset</button>
`,
})
export class CounterControls {
protected readonly counter = injectBusDispatch(counterPageEvents);
}Like any inject* function, it needs an injection context, such as a field initializer or a constructor. If you need to call it somewhere else, pass an injector explicitly:
const counter = injectBusDispatch(counterPageEvents, { injector: this.injector });BusDispatcher
Under the hood, injectBusDispatch uses BusDispatcher, a root service that dispatches any event object. You can inject it directly, which is handy in services and facades:
import { inject, Service } from '@angular/core';
import { BusDispatcher } from '@busly/angular';
@Service()
export class CounterFacade {
readonly #dispatcher = inject(BusDispatcher);
set(count: number): void {
this.#dispatcher.dispatch(counterPageEvents.set(count));
}
}Source
By default, events sent from Angular carry the source 'busly-angular'. To give your application its own label, provide it at bootstrap:
import { provideBusDispatcher } from '@busly/angular';
bootstrapApplication(App, {
providers: [provideBusDispatcher({ source: 'shell' })],
});Dispatch from the browser
Busly events are meant for live communication in the browser, so dispatch them from code that runs there: template event handlers, afterNextRender callbacks, or services reacting to user actions.
@Component({ /* ... */ })
export class Shell {
readonly #shell = injectBusDispatch(shellEvents);
constructor() {
afterNextRender(() => this.#shell.ready());
}
}Dispatching from a constructor would also work, since the adapter does nothing on the server, but afterNextRender makes your intent much clearer.
Listening to events in Angular
To listen, inject BusEvents, another root service. Its on(...) and lazy(...) methods return plain Observables of the events you ask for, which you can consume in whichever way suits the component.
Subscribe
The most direct option is to subscribe and tear down with takeUntilDestroyed:
import { Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { BusEvents } from '@busly/angular';
@Component({ /* ... */ })
export class CounterLog {
constructor() {
inject(BusEvents)
.on(counterPageEvents.set)
.pipe(takeUntilDestroyed())
.subscribe(({ payload, source }) => console.log(`${source}: ${payload}`));
}
}Whatever approach you choose, make sure the subscription is torn down, whether through takeUntilDestroyed, the async pipe, toSignal, or a manual unsubscribe.
As a signal
If the component only needs the latest value, toSignal turns the stream into a signal:
import { toSignal } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
@Component({
template: `<p>Count: {{ count() }}</p>`,
})
export class CounterView {
protected readonly count = toSignal(
inject(BusEvents).on(counterPageEvents.set).pipe(map(({ payload }) => payload)),
{ initialValue: 0 },
);
}With the async pipe
You can also hand the Observable straight to the template and let the async pipe manage the subscription:
@Component({
imports: [AsyncPipe],
template: `@if (last$ | async; as last) { <p>{{ last.payload }}</p> }`,
})
export class CounterView {
protected readonly last$ = inject(BusEvents).on(counterPageEvents.set);
}Several events
To listen to more than one event, pass several creators. Their streams are merged, and the event type narrows to a union you can branch on:
inject(BusEvents)
.on(counterPageEvents.increment, counterPageEvents.set)
.pipe(takeUntilDestroyed())
.subscribe((event) => {
if (event.type === counterPageEvents.set.type) {
this.count.set(event.payload);
} else {
this.count.update((n) => n + 1);
}
});Calling on() without any creators throws an error.
Zone-based apps
Zoneless applications, which are the default in Angular 22, don't need anything special.
If your app still uses zone.js, there's one thing to watch for. Events dispatched by code outside Angular, such as a microfrontend written in another framework on the same page, reach your listeners outside the Angular zone, so change detection may not run. Writing to signals solves this, because signals schedule change detection on their own. Otherwise, wrap the work in NgZone.run.
The on / lazy event handlers in Angular
The two cohorts from on and lazy are simply two methods on BusEvents. NgRx handles the same split with two separate services, so here's how they map:
| Concern | Busly | NgRx Signals Events |
|---|---|---|
| Reach | Same-origin contexts | This JS context only |
| Emit | BusDispatcher / injectBusDispatch | Dispatcher / injectDispatch |
| Listen, state | BusEvents.on(...) | ReducerEvents.on(...) |
| Listen, effects | BusEvents.lazy(...) | Events.on(...) |
In the example below, the effect is subscribed before the state listener on purpose. Even so, the signal is already updated by the time the HTTP call reads it:
@Component({ /* ... */ })
export class Counter {
readonly #events = inject(BusEvents);
readonly #http = inject(HttpClient);
protected readonly count = signal(0);
constructor() {
// effect, subscribed first on purpose
this.#events
.lazy(counterPageEvents.set)
.pipe(
switchMap(() => this.#http.put('/api/count', this.count())),
takeUntilDestroyed(),
)
.subscribe();
// state
this.#events
.on(counterPageEvents.set)
.pipe(takeUntilDestroyed())
.subscribe(({ payload }) => this.count.set(payload));
}
}
// set(42): count.set(42) → PUT 42For this to work, write state synchronously in your on listeners, with signal.set or patchState, just as you would in an NgRx reducer. A write that happens after some asynchronous work loses the ordering.
The order holds within one BusEvents instance. If several Angular applications run on the same page, each has its own instance and its own ordering.
Behind the scenes, all listeners for a given event type share a single subscription to the bus, which is released when the last one unsubscribes. And if one listener throws, the others still run.
Using on NgRx projects
Busly is designed to sit next to NgRx, not to replace its event bus. The two cover different ground: NgRx events stay inside one application, while Busly events travel across applications, tabs, and frameworks. Decide for each event which bus it belongs on. The same creators work with both.
Don't use NgRx's features with Busly
It's tempting to plug Busly events into NgRx's own store features, but two of them don't play well together.
withReducer only listens to NgRx's ReducerEvents, so it never sees Busly events at all.
withEventHandlers is trickier. It dispatches into NgRx every emitted value that looks like an event, unless that value carries an internal NgRx tag. Busly events don't carry the tag, so a handler that simply passes a Busly event through, as in events.on(x).pipe(tap(...)), ends up re-dispatching it into NgRx. If both buses use the same event types, your reducers run twice, and a handler that forwards NgRx events back to Busly can create a loop.
That's why Busly provides its own versions of these features, covered in Using with signalStore.
Reusing ngrx event creators
If your project already defines events with @ngrx/signals/events, there's no need to redefine them. Those creators have exactly the shape Busly needs, so you can keep them and skip @busly/events:
import { type } from '@ngrx/signals';
import { eventGroup } from '@ngrx/signals/events';
export const counterPageEvents = eventGroup({
source: 'Counter Page',
events: {
increment: type<void>(),
set: type<number>(),
},
});The same group can then be sent on either bus, depending on who needs to hear it:
// same group, either bus
const local = injectDispatch(counterPageEvents); // NgRx, this app
const shared = injectBusDispatch(counterPageEvents); // Busly, every contextApplications on the other side, written in React or plain JavaScript, can define the same group with @busly/events. As long as the type strings match, they'll understand each other.
Using with signalState
signalState doesn't come with any event features of its own, but it's easy to drive from Busly. Listen with on and patch the state in the subscription:
import { Component, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { patchState, signalState } from '@ngrx/signals';
import { BusEvents } from '@busly/angular';
@Component({
template: `<p>{{ state.count() }}</p>`,
})
export class Counter {
protected readonly state = signalState({ count: 0 });
constructor() {
const events = inject(BusEvents);
events
.on(counterPageEvents.set)
.pipe(takeUntilDestroyed())
.subscribe(({ payload }) => patchState(this.state, { count: payload }));
events
.on(counterPageEvents.increment)
.pipe(takeUntilDestroyed())
.subscribe(() => patchState(this.state, ({ count }) => ({ count: count + 1 })));
}
}Because patchState is synchronous, any lazy listener for the same event will already see the patched state.
Using with signalStore
For SignalStore, @busly/ngrx provides Busly versions of NgRx's withReducer, on, and withEventHandlers.
pnpm add @busly/ngrx @busly/angular @busly/core @ngrx/signalsnpm install @busly/ngrx @busly/angular @busly/core @ngrx/signalsbun add @busly/ngrx @busly/angular @busly/core @ngrx/signalsIt only needs the core @ngrx/signals package, not @ngrx/signals/events.
Registering reducers
State transitions are declared with withBusReducer and on, in the same way you'd use NgRx's withReducer:
import { signalStore, withState } from '@ngrx/signals';
import { on, withBusReducer } from '@busly/ngrx';
export const CounterStore = signalStore(
withState({ count: 0, dirty: false }),
withBusReducer(
// partial state
on(counterPageEvents.set, ({ payload }) => ({ count: payload })),
// updater
on(counterPageEvents.increment, () => (state) => ({ count: state.count + 1 })),
// reducer also gets current state
on(counterPageEvents.loadSuccess, ({ payload }, state) => ({ count: state.count + payload.count })),
// several events, array of partials / updaters
on(counterPageEvents.set, counterPageEvents.increment, () => [{ dirty: true }]),
),
);As the example shows, a reducer can return a partial state, an updater function, or an array mixing both, and the result is applied with patchState. Each on needs at least one event creator.
Reducers listen through BusEvents.on, so they always run before any lazy listener, regardless of the order of features in the store. They stop listening when the store's injector is destroyed.
Registering event handlers
Side effects go in withBusEventHandlers, which mirrors NgRx's withEventHandlers but dispatches on the bus:
import { inject } from '@angular/core';
import { signalStore, withState } from '@ngrx/signals';
import { BusEvents } from '@busly/angular';
import { on, withBusEventHandlers, withBusReducer } from '@busly/ngrx';
import { map, switchMap, tap } from 'rxjs';
export const CounterStore = signalStore(
withState({ count: 0 }),
withBusReducer(on(counterPageEvents.set, ({ payload }) => ({ count: payload }))),
withBusEventHandlers((store, events = inject(BusEvents), api = inject(CounterApi)) => ({
// new event → dispatched on the bus, reaches every context
save$: events.lazy(counterPageEvents.set).pipe(
switchMap(() => api.save(store.count())), // reads reduced state
map((count) => counterPageEvents.loadSuccess({ count })),
),
// received event passed through → never re-dispatched
log$: events.lazy(counterPageEvents.set).pipe(tap((e) => console.log(e.source))),
})),
);The factory returns either a record or an array of Observables. What happens to the values they emit depends on what those values are:
| Emitted value | Result |
|---|---|
New event ({ type, payload } you created) | Dispatched via BusDispatcher |
Event received from BusEvents, passed through | Ignored, no loop |
Spread copy of a received event ({ ...e }) | New event → dispatched |
Primitive, object without payload, tuple | Ignored |
Listen with lazy in your handlers, so they read the state your reducers have already updated.
Be careful with spread copies. A copy like { ...e } counts as a new event, so if it keeps the same type as the event it came from, it'll be dispatched again and again in an endless loop.
NgRx's [event, { scope }] tuples are ignored, because Busly has no scopes.
Each handler is subscribed on its own, so an error ends only the handler that failed, whereas NgRx would end every handler in the feature. All handlers are torn down when the store's injector is destroyed.
Server-side rendering
On the server, the adapter steps aside. BusDispatcher.dispatch and injectBusDispatch send nothing, and BusEvents.on and lazy return an Observable that completes immediately.
Hydration doesn't need any special handling. The server renders the initial state, the browser hydrates, and Busly events take over from there. If the server-rendered HTML needs to show some piece of state, load it through the usual means: resolvers, initial store state, or an HTTP call during rendering.