Skip to content

API Reference

SynkroModule

SynkroModule.forRoot(options)

Registers the module with static configuration. Returns a global DynamicModule.

SynkroModule.forRoot({
transport: "redis",
connectionUrl: "redis://localhost:6379",
workflows: [],
retention: { lockTtl: 30 },
});

SynkroModule.forRootAsync(options)

Registers the module with async configuration resolved from injected providers.

SynkroModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
transport: "redis",
connectionUrl: config.getOrThrow("REDIS_URL"),
}),
});
PropertyTypeDescription
importsany[]Modules to import for provider resolution.
injectany[]Tokens to inject into useFactory.
useFactory(...args) => SynkroModuleOptions | Promise<SynkroModuleOptions>Factory function that returns configuration.

SynkroService

Injectable service that wraps the core Synkro instance. Available globally after module registration.

publish(event, payload?, requestId?)

Publishes an event to all registered handlers and triggers matching workflows.

const id = await synkroService.publish("order.created", { orderId: "123" });
ParameterTypeDescription
eventstringEvent type.
payloadunknownOptional event payload.
requestIdstringOptional deduplication ID. Auto-generated if omitted.

Returns Promise<string> — the request ID.

on(eventType, handler, retry?)

Registers an event handler programmatically (alternative to @OnEvent).

ParameterTypeDescription
eventTypestringEvent type to listen for.
handlerHandlerFunctionCallback invoked with the event payload.
retryRetryConfigOptional retry configuration.

introspect()

Returns metadata about all registered events and workflows.

Returns SynkroIntrospection

getEventMetrics(eventType)

Returns metrics (received, completed, failed) for a specific event type.

Returns Promise<EventMetrics>

getInstance()

Returns the underlying Synkro instance for advanced use cases.

Returns Synkro

Injection example

import { Controller, Post, Body } from "@nestjs/common";
import { SynkroService } from "@synkro/nestjs";
@Controller("orders")
export class OrdersController {
constructor(private readonly synkro: SynkroService) {}
@Post()
async createOrder(@Body() body: { items: string[] }) {
const requestId = await this.synkro.publish("order.created", body);
return { requestId };
}
}

Decorators

@OnEvent(type, retry?)

Method decorator. Registers the decorated method as a handler for the given event type.

ParameterTypeDescription
typestringEvent type.
retryRetryConfigOptional retry configuration (maxRetries, retryDelay).

@OnWorkflowStep(workflow, step)

Method decorator. Binds the decorated method as the handler for a workflow step.

ParameterTypeDescription
workflowstringWorkflow name.
stepstringStep type within the workflow.