Nitrocloud LogoNitroStack
/sdk
/typescript
/events

Events Guide

Emitting Events

@Tool({ name: 'create_order' })
async createOrder(input: any, ctx: ExecutionContext) {
  const order = await this.orderService.create(input);
  
  // Emit event
  ctx.emit('order.created', {
    orderId: order.id,
    userId: ctx.auth?.subject,
    total: order.total
  });
  
  return order;
}

Listening to Events

@Injectable()
export class NotificationService {
  @OnEvent('order.created')
  async handleOrderCreated(data: any) {
    await this.emailService.sendOrderConfirmation(data.userId, data.orderId);
  }
  
  @OnEvent('user.registered')
  async handleUserRegistered(data: any) {
    await this.emailService.sendWelcomeEmail(data.email);
  }
}

Event Patterns

Naming Convention

Use dot notation:

  • `user.created`
  • `order.updated`
  • `payment.failed`

Event Data

Include relevant context:

ctx.emit('product.purchased', {
  productId: product.id,
  userId: ctx.auth?.subject,
  price: product.price,
  timestamp: new Date()
});

Best Practices

  1. Async handlers - Don't block tool execution
  2. Error handling - Catch errors in handlers
  3. Naming convention - Use consistent patterns
  4. Documentation - Document what events are emitted

Next Steps