Skip to content

The #[AsyncApi] Attribute

The #[AsyncApi] PHP attribute is the core of the package. Add it to any Laravel broadcast event class to include it in the generated AsyncAPI specification.

Basic Usage

php
use Victormgomes\AsyncApi\Attributes\AsyncApi;

#[AsyncApi]
class UserRegistered implements ShouldBroadcast
{
    // ...
}

With no parameters, the package will infer the channel name from the event's broadcastOn() method and use the event class name as the operation name.

Attribute Parameters

ParameterTypeDefaultDescription
channel?stringnullThe broadcast channel URI. Supports dynamic parameters like chat.{room}.
dto?stringnullFully qualified class name of a DTO/Model to extract the payload schema from.
descriptionstring''Human-readable description of the operation.
name?stringnullCustom name for the event. Defaults to the class short name or broadcastAs().
summary?stringnullShort summary of the operation (AsyncAPI 3.0).
operationId?stringnullUnique identifier for the operation. Defaults to {action}{eventName}.
actionstring'send'The operation action: 'send' or 'receive'.
tagsarray[]Tags for grouping operations in the specification.
examplesarray[]Example payloads for the message.
bindingsarray[]Protocol-specific bindings (e.g., WebSocket configuration).
externalDocs?arraynullExternal documentation link.
correlationId?stringnullCorrelation ID location for message tracing.
security?arraynullCustom security schemes for this specific operation.

DTO Schema Extraction

When you specify a dto, the package uses reflection to extract all public properties and their types, generating a JSON Schema for the message payload.

php
#[AsyncApi(dto: ChatMessageDTO::class)]
class ChatMessage implements ShouldBroadcast { /* ... */ }

The ChatMessageDTO class should be a simple class with typed public properties (or extend Spatie\LaravelData\Data):

php
<?php

declare(strict_types=1);

namespace App\DTOs;

class ChatMessageDTO
{
    public function __construct(
        public string $message,
        public string $sender,
        public ?string $timestamp = null,
    ) {}
}

If no dto is specified, the package attempts to auto-discover the payload by:

  1. Looking for a constructor parameter named $data
  2. Checking for Spatie\LaravelData\Data subclasses
  3. Guessing DTO class names based on naming conventions ({Event}DTO, {Event}EventDTO)

Channel Inference

If channel is not specified, the package reads the event source file and attempts to extract the channel from new Channel(...), new PrivateChannel(...), or new PresenceChannel(...) calls in the broadcastOn() method.

Dynamic channel parameters (e.g., chat.{room}) are automatically detected and documented as AsyncAPI channel parameters.

Action

The action parameter specifies the direction of the message flow:

  • 'send' (default): The application sends this message to the channel
  • 'receive': The application receives this message from the channel