ringierimu/event-bus

This package is abandoned and no longer maintained. No replacement package was suggested.

Send Laravel Events to Event Bus

1.0.0 2023-08-15 16:58 UTC

This package is auto-updated.

Last update: 2023-10-20 21:15:59 UTC


README

Build Status StyleCI Total Downloads Latest Stable Version License

Introduction

Laravel Event Bus provides a simple interface that provides a Laravel event the ability to be dispatched onto Ringier Event Bus.

Installation

Install the package using composer:

composer require ringierimu/event-bus

Next publish the config file using:

php artisan vendor:publish --provider=Ringierimu\EventBus\EventBusServiceProvider

This will create a config file at config/event-bus.php. Feel free to browse the file and update it as required by your application.

Update your .env file with the following variable substitution the correct values for your application:

RINGIER_SB_NODE_ID=123456789
RINGIER_SB_USER=event_bus_user
RINGIER_SB_PASSWORD=event_bus_password

You are encouraged to have a further look at the config/event-bus.php file to learn more about other available configurations.

Usage

To make a Laravel Event dispatchable onto the bus you only need to have your event class extend the Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus interface. You then need to implement the toEventBus method on the Event class; this will allow you to configure how the event will be sent to the bus as. E.g payload, eventType, action and so on.

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
}

Finally, just dispatch you event as you would any normal Laravel event. Your event will now be dispatched onto the bus.

namespace App\Http\Controllers;

use App\Events\ListingCreatedEvent;
use App\Http\Requests\StoreListingRequest;
use App\Models\Listing;
use Illuminate\Http\RedirectResponse;

class ListingController extends Controller
{
    /**
     * Store a new Listing.
     * 
     * @return RedirectResponse
     */
    public function store(StoreListingRequest $request): RedirectResponse
    {
        $listing = Listing::create($request->validated());
    
        // Event will automatically be dispatched onto the
        // bus as well.
        ListingCreatedEvent::dispatch($listing);
        
        return back();
    }
}

Customising the Event type

By default, event type is being sent as the name of the event class. However, you can customise the type name by using the withEventType method of the Ringierimu\EventBus\Event class. In your toEventBus method do the following:

/**
 * Get the representation of the event for the EventBus.
 *
 * @param  Event  $event
 * @return Event
 */
public function toEventBus(Event $event): Event
{
    return $event
        ->withEventType('UserListingCreatedEvent')
        ->withPayload([
            'id' => $this->listing->id,
            'title' => $this->listing->title,
            'description' => $this->listing->description
        ]);
}

You may also implement a broadcastToEventBusAs method on your Laravel Event class, however note that withEventType will take precedence over broadcastToEventBusAs.

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
    
    /**
     * Get the event type name being sent to the event bus.
     *
     * @return string
     */
    public function broadcastToEventBusAs(): string
    {
        return 'UserListingCreatedEvent';
    }
}

Customising the queue

By default, all event bus events being sent are processed on queue. Your default queue and connection will be used for sending dispatching the jobs, however you can specify a dedicated queue and connection for processing your events by adding the following to your .env:

RINGIER_SB_QUEUE=eventbus
RINGIER_SB_QUEUE_CONNECTION=redis

Alternatively you can specify the queue and connection on a per-Event basis by adding the onQueue and onConnection methods to your Laravel Event classes.

namespace App\Events

use App\Models\Listing;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Ringierimu\EventBus\Contracts\ShouldBroadcastToEventBus;
use Ringierimu\EventBus\Event;

class ListingCreatedEvent implements ShouldBroadcastToEventBus
{
    use Dispatchable, SerializesModels;

    /**
     * Create an instance of ListingCreated event.
     *
     * @param  Listing  $listing
     */
    public function __construct(
        public Listing $listing
    ) {
    }

    /**
     * Get the representation of the event for the EventBus.
     *
     * @param  Event  $event
     * @return Event
     */
    public function toEventBus(Event $event): Event
    {
        return $event
            ->withPayload([
                'id' => $this->listing->id,
                'title' => $this->listing->title,
                'description' => $this->listing->description
            ]);
    }
    
    /**
     * Specify the queue name on which this event should be processed.
     *
     * @param  Event  $event
     * @return string
     */
    public function onQueue(Event $event): string
    {
        return 'eventbus';
    }
    
    /**
     * Specify the queue connection on which this event should be processed.
     *
     * @param  Event  $event
     * @return string
     */
    public function onConnection(Event $event): string
    {
        return 'redis';
    }
}

Testing

phpunit