#the pusher does not work, events do not seem to be sent. Laravel 11 + React + pusher

3 messages · Page 1 of 1 (latest)

solar dock
#

I use pusher as a broadcasting driver and laravel echo on the frontend to listen to events. There are no errors, and the connection seems to be established, but when I send an event event(new StoreMessageEvent($message)), the broadcastingOn() method is not executed (I checked this by writing Log::info()). Also, on the frontend, using echo, I listen to the channel and tried to send events to the pusher through the debug console so that they are read using echo, but nothing happens.

#

Chat/index.jsx:

import echo from '../../../echo'
const { id } = useParams()
  useEffect(() => {
    echo.channel(`chat.${id}`).listen('.store_message', e => console.log(e))
  }, [])

echo.js:


import Echo from 'laravel-echo'
import Pusher from 'pusher-js'

window.Pusher = Pusher

const echo = new Echo({
  broadcaster: 'pusher',
  key: import.meta.env.VITE_PUSHER_APP_KEY,
  cluster: import.meta.env.VITE_PUSHER_APP_CLUSTER,
  forceTLS: true,
})

export default echo

StoreMessageEvent.php:

<?php

namespace App\Events;

use App\Http\Resources\MessageResource;
use App\Models\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Support\Facades\Log;

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

    /**
     * Create a new event instance.
     */
    public function __construct(private Message $message)
    {
    }

    public function broadcastAs(): string
    {
        return 'store_message';
    }

    public function broadcastOn(): array
    {
        Log::info('Broadcasting on channel: chat.' . $this->message->chat_id);
        return [
            new Channel('chat.' . $this->message->chat->id),
        ];
    }

    public function broadcastWith(): array
    {
        Log::info('Broadcasting message: ', ['message' => MessageResource::make($this->message)->resolve()]);
        return [
            'message' => MessageResource::make($this->message)->resolve()
        ];
    }
}
#

MessageService.php:

 public function store(MessageRequest $request, Chat $chat)
    {
        $data = $request->validated();

        $data['user_id'] = auth()->user()->id;

        $data['status'] = MessageStatus::SENT->value;

        $message = $chat->messages()->create($data);

        event(new StoreMessageEvent($message));

        Log::info('Message created and event fired', ['message' => $message]);

        return $message;
    }

bootstrap/app.php:

<?php

use App\Http\Middleware\CheckTokenAbilities;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
use Laravel\Sanctum\Http\Middleware\CheckForAnyAbility;

return Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__ . '/../routes/web.php',
        api: __DIR__ . '/../routes/api.php',
        channels: __DIR__ . '/../routes/channels.php',
        commands: __DIR__ . '/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware) {
        $middleware->alias(['ability' => CheckForAnyAbility::class]);
    })
    ->withExceptions(function (Exceptions $exceptions) {
        //
    })->create();