ably / laravel-broadcaster

Official Laravel Ably Broadcaster
https://laravel.com/docs/broadcasting
Apache License 2.0
49 stars 8 forks source link

Class "App\Jobs\Ably\AblyRest" not found #help #54

Open madfortech opened 2 weeks ago

madfortech commented 2 weeks ago
<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Ably\AblyRest;

class AblyJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private $message;
    private $channelName;

    /**
     * Create a new job instance.
     */
    public function __construct($message, $channelName)
    {
        $this->message = $message;
        $this->channelName = $channelName;
    }

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $ably = new Ably\AblyRest(env('ABLY_KEY'));
        $channel = $ably->channels->get($this->channelName);
        $channel->publish('message', $this->message);
    }
}
<?php

namespace App\Livewire\Message;

use Livewire\Component;
use App\Models\Message;
use Illuminate\Support\Facades\Auth;
use Illuminate\Contracts\View\View;
use Livewire\Attributes\Validate;
use Livewire\WithFileUploads;
use App\Events\Message\PublicMessageEvent;
use App\Models\User;
use App\Jobs\AblyJob;

class CreateMessage extends Component
{

    use WithFileUploads;

    public int $userId;
    #[Validate('required', message: 'Please provide a message')]
    public $body;
    public $file;
    public $channelName = 'public-channel'; // Ably channel name

    public function rules() 
    {
        return [
            'body' => 'required',
            'file' => 'nullable|max:2048',
        ];
    }

    public function save() 
    {
        $this->validate(); // Validate the data

        if ($this->file) {
            $message = Message::create([
                'user_id' => Auth::id(),  
                'body' => $this->body,
            ]);
            foreach ($this->file as $file) {
                $message->addMedia($file->getRealPath())->toMediaCollection('media');
            }
        } else {
            $message = Message::create([
                'user_id' => Auth::id(),  
                'body' => $this->body,
            ]);
            AblyJob::dispatch($message, 'public-channel');
        }

        $channel = $ably->channels->get($this->channelName);
        $channel->publish('message', $message);

        broadcast(new PublicMessageEvent($this->channelName, $message));

        // Random user select karne ke liye
        $randomUser = User::inRandomOrder()->first();
        $this->userId = $randomUser->id;

        $this->body = '';
        $this->file = [];
        return  back();
    }

    public function render():View
    {
        $messages = Message::with('user')
        ->latest()
        ->take(10)
        ->get()
        ->sortBy('id');   
        return view('livewire.message.create-message',compact('messages'));
    }

    // Delete 
    public function delete(int $messageId): void
    {

        $message = Message::where('id', $messageId)
        ->where('user_id', Auth::id()) // Ensure message belongs to the current user
        ->first();

        // Delete the message if it exists
        if ($message) {
            $message->clearMediaCollection('media');
            $message->delete();
        }

    }

    public function rotateRandomUser()
    {
        $randomUser = User::inRandomOrder()->first();
        $this->userId = $randomUser->id;
    }

    public function publishMessage($message)
    {
        $ably = new AblyRealtime(env('ABLY_KEY'));
        $channel = $ably->channels->get($this->channelName);
        $channel->publish('message', $message);
    }
}

ably/ably-php": "^1.1",

ABLY_KEY=my key BROADCAST_DRIVER=ably ABLY_DISABLE_PUBLIC_CHANNELS=true ABLY_TOKEN_EXPIRY=21600 ABLY_SYNC_SERVER_TIME=true

Capture Capture1

Php version PHP 8.1.17 Laravel Framework 10.48.22 Any one help me i will make a project use Laravel

┆Issue is synchronized with this Jira Task by Unito

sacOO7 commented 2 weeks ago

Not sure why you need to create AblyRest instance

    /**
     * Execute the job.
     */
    public function handle(): void
    {
        $ably = new Ably\AblyRest(env('ABLY_KEY'));
        $channel = $ably->channels->get($this->channelName);
        $channel->publish('message', $this->message);
    }

Broadcaster automatically create instance here, you might like to reuse the same -> https://github.com/ably/laravel-broadcaster/blob/082d4e01b81048d33e8b2d04d998d6e70f3bb057/src/LaravelAblyBroadcasterServiceProvider.php#L16-L24.

and we don't support realtime instance creation

    public function publishMessage($message)
    {
        $ably = new AblyRealtime(env('ABLY_KEY'));
        $channel = $ably->channels->get($this->channelName);
        $channel->publish('message', $message);
    }
sacOO7 commented 2 weeks ago

I feel you might like to refer to official doc https://laravel.com/docs/10.x/broadcasting, on how to use laravel-broadcaster as a part of jobs mechanism. I don't feel need to create separate AblyRest instance, instead it should be inferred from laravel-broadcaster/src/LaravelAblyBroadcasterServiceProvider.php