Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
madfortech opened this issue Nov 6, 2024 · 2 comments
Open

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

madfortech opened this issue Nov 6, 2024 · 2 comments

Comments

@madfortech
Copy link

madfortech commented Nov 6, 2024

<?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](https://github.com/user-attachments/assets/fa1c97e8-8673-47c9-88bb-9964dec59688)
![Capture1](https://github.com/user-attachments/assets/9225e54f-7143-40ab-978f-e97c317b668b)

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](https://ably.atlassian.net/browse/ECO-5092) by [Unito](https://www.unito.io)
@sacOO7
Copy link
Collaborator

sacOO7 commented Nov 6, 2024

@madfortech For laravel broadcaster, please go through the doc -> https://github.com/ably/laravel-broadcaster

@sacOO7
Copy link
Collaborator

sacOO7 commented Nov 6, 2024

It seems, you are directly using AblyRest instance, you might like to use https://github.com/ably/ably-php-laravel package instead. Also, ably-php only supports REST API, it doesn't support REALTIME.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants