duckdivesurvive's avatar

Consume Pusher events broadcast by Application A in Application B

Hi

I want to broadcast an event in application A using pusher as the broadcast driver. All this is working as i can see the message number increase in the pusher dashboard.

In application B i want to then consume these events using the worker queue and at the moment just log it to laravel log. once i know its working i can do what i need to.

Application A can broadcast and consume events - i have tested this

Application B can do the same and again i have tested this

Both are using the same pusher credentials

Application A is laravel 9 and Application B is version 11

What i cannot get to work is Application A broadcasts an event and then Application B consumes it.

Application A code


namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class AllDone implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('channel-name');
    }
}

Where i broadcast the event

 event(new AllDone());

Application B

Here i created a class in the Events folder (but not an event via the cli) so i can then register it as the event to listen for in the EventServiceProvider

<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class AllDone
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     */
    public function __construct()
    {
        //
    }
}

The Listener class (created via the cli command )

<?php

namespace App\Listeners;

use App\Events\AllDone;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Log;

class HandleAllDone
{
    /**
     * Create the event listener.
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     */
    public function handle(AllDone $event): void
    {
        //
        Log::info('AllDone received ');

    }
}

In EventServiceProvider

<?php

namespace App\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        'App\Events\AllDone' => [
            'App\Listeners\HandleAllDone',
        ],
        'App\Events\TestEvent' => [
            'App\Listeners\HandleTestEvent',
        ],
    ];

    /**
     * Register any events for your application.
     */
    public function boot(): void
    {
        //
    }

    /**
     * Determine if events and listeners should be automatically discovered.
     */
    public function shouldDiscoverEvents(): bool
    {
        return false;
    }
}

then i run

php artisan queue:work

i have got it to the point where if i use pusher-js and livewire on application B (which i dont want to do ) as a test the event is logged however the above example gives an error

[2024-07-04 14:54:27] local.ERROR: Call to undefined method App\Events\AllDone::broadcastOn() {"exception":"[object] (Error(code: 0): Call to undefined method App\Events\AllDone::broadcastOn() at /Users/duckdivesurvive/Development/testapp/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php:79)

This error is clear as i created a basic class for the the event and not an actual event , not sure the work around or fix

I simply get no log messages , i dont need a web frontend for Application B it just needs to pick up the messages and process them.

0 likes
1 reply
vincent15000's avatar

I've not read all your post.

If you have an application sending messages via Pusher, you have just to listen to the Pusher channel from another application.

You just have to be connected with both applications to Pusher.

This should not be a problem.

Please or to participate in this conversation.