Skip to main content

DI Integration

The ProxyEventBusResolver class provides an IEventBusResolver whose underlying resolver is resolved from a DI container.

The proxy supports the IEventBusResolver operations:

  • use(adapterName?) returns a dispatcher that resolves the resolver from the container for each dispatch. This supports the resolver's configured lifetime; use scoped registrations inside container.run().
  • addListener(eventNames, listener) registers the listener as a container initialization hook. Listeners must be registered before container.init(); you cannot add listeners dynamically after initialization.

Create the proxy before calling container.init(). The resolver is initialized by the container, and listeners registered through the proxy are added during container initialization. In a multi-server deployment, register listeners during startup on each server so each server is configured to receive events.

Initial configuration​

Set up the memory and Redis adapters, then create the container and register the EventBusResolver. Wrap it with ProxyEventBusResolver and register listeners before initializing the container:

1. Set up the memory event-bus adapter​

./samples/memory-event-bus-adapter.ts
import { MemoryEventBusAdapter } from "eridu-tech/event-bus/memory-event-bus-adapter";

export const memoryEventBusAdapter = new MemoryEventBusAdapter();

2. Set up the Redis event-bus adapter​

./samples/redis-event-bus-adapter.ts
import { RedisPubSubEventBusAdapter } from "eridu-tech/event-bus/redis-pub-sub-event-bus-adapter";
import { SuperJsonSerde } from "eridu-tech/serde/super-json-serde";
import { Redis } from "ioredis";

export const redisEventBusAdapter = new RedisPubSubEventBusAdapter({
client: new Redis("YOUR_REDIS_CONNECTION_STRING"),
serde: new SuperJsonSerde(),
});

3. Create the dependency injection container​

./samples/container.ts
import { Container } from "eridu-tech/di";
import { ExecutionContext } from "eridu-tech/execution-context";
import { AlsExecutionContextAdapter } from "eridu-tech/execution-context/als-execution-context-adapter";

export const executionContext = new ExecutionContext(
new AlsExecutionContextAdapter(),
);
export const container = new Container({ executionContext });

4. Register and initialize the event-bus resolver​

./samples/event-bus-resolver-di.ts
import { genericToken, LIFETIME } from "eridu-tech/di/contracts";
import { ProxyEventBusResolver } from "eridu-tech/event-bus/di";
import { EventBusResolver } from "eridu-tech/event-bus";
import { container } from "./container.js";
import { memoryEventBusAdapter } from "./memory-event-bus-adapter.js";
import { redisEventBusAdapter } from "./redis-event-bus-adapter.js";

type Adapters = "memory" | "redis";
type EventMap = {
userCreated: {
userId: string;
};
};

const resolverToken =
genericToken<EventBusResolver<Adapters, EventMap>>("eventBusResolver");
container.registerFactory({
token: resolverToken,
factory: () =>
new EventBusResolver<Adapters, EventMap>({
adapters: {
memory: memoryEventBusAdapter,
redis: redisEventBusAdapter,
},
defaultAdapter: "memory",
}),
onInit: async (resolver) => {
await resolver.init();
},
deps: {},
lifetime: LIFETIME.SINGLETON,
});

// Create the proxy before container.init()
export const eventBus = new ProxyEventBusResolver<Adapters, EventMap>({
container,
resolverToken,
});

// Register listeners during application setup, before container.init()
await eventBus.addListener("userCreated", (event) => {
console.log(event.userId);
});

await container.init();

Usage​

1. Using the default adapter​

./samples/event-bus-resolver-di-default-adapter.ts
import { eventBus } from "./event-bus-resolver-di.js";

// Uses the adapter configured as the default (memory)
await eventBus.use().dispatch("userCreated", { userId: "user-1" });

2. Specifying an adapter explicitly​

./samples/event-bus-resolver-di-specific-adapter.ts
import { eventBus } from "./event-bus-resolver-di.js";

await eventBus.use("redis").dispatch("userCreated", { userId: "user-1" });

Further information​

For further information refer to eridu-tech/event-bus API docs.