I created a new event, and signed up at pusher in order to test my notifications.
This is how my controller looks like:
public function view_posts(){
$user = User::find(6999);
event(new TestNotification($user));
return $user;
}
And inside the TestNotification I initialized my constructor:
<?php
namespace App\Events;
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;
class TestNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public function __construct($user)
{
//
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
I have my .env setup. ( All the details are fake )
BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=database
PUSHER_APP_NAME=testapp
PUSHER_APP_ID=2655265
PUSHER_APP_KEY=ffgf5ghfgh1g5
PUSHER_APP_SECRET=6rergf4erg4er
PUSHER_APP_CLUSTER=us2
When I visit my url, the user gets returned, fine, but when I go to pusher console, I don't see the user array or anything related to the notification.
Note: If I change my broadcast_driver to log, it doesn't work either
Is there anything that I am possibly missing?
Related
I used pusher for my project. I configure broadcasting as per laravel docs. When I fired my event pusher does not work for me. But when I send data from pusher console then pusher receive this data. I also try vinkla/pusher. Its work fine but laravel event broadcasting not work. It's not broadcast my messages. Please help me.
Here is my Event
<?php
namespace App\Events;
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;
class ChatNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($message)
{
$this->message=$message;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
dd($this->message);
$GroupId=$this->message->fk_group_id;
if(!empty($GroupId) && !is_null($GroupId)){
return new PresenceChannel('Room.'.$GroupId);
}else{
return new PrivateChannel('Chat.'.$this->message->to_id);
}
}
}
I have this Job
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Http\Traits\CreateTrait;
class CreateJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, CreateTrait;
protected $contactForm;
public function __construct($contactForm)
{
$this->contactForm = $contactForm;
}
public function handle()
{
//This connects to SSH and takes 5-10s
$this->CreateOnetime($this->contactForm->room_id, $this->contactForm->company_id, $this->contactForm->id);
}
}
Then I try in controller
use App\Jobs\CreateJob;
class ContactFormController extends Controller
{
public function save(StoreContactFormRequest $request, $cid, $rid){
$validated = $request->validated();
$validated['room_id'] = $rid;
$validated['company_id'] = $cid;
$contactForm = ContactForm::create($validated);
CreateJob::dispatch($contactForm);
return back();
}
}
Nothing written in the DB, though I have QUEUE_DRIVER=database in the .env file.
The function runs synchronously, so it does not create the Job somehow, just runs it.
Problem was I had QUEUE_CONNECTION=sync somewhere after QUEUE_DRIVER=database
Sorry if this is a noob question.
I used to send emails directly through the controller, like so:
Mail::to($user)->send(new RegisterConfirmation($user));
As you can see I also pass the $user into the email where I can use that user's information, like "Hello {{$user->username}}!"
But now instead I want to queue emails, so in my controller I now have this:
RegisterConfirmationJob::dispatch($user);
In my job, I have the following:
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
When I run this, it returns the following error:
[2021-07-09 20:33:57] local.ERROR: Undefined property:
App\Jobs\RegisterConfirmationJob::$user {"exception":"[object]
(ErrorException(code: 0): Undefined property:
App\Jobs\RegisterConfirmationJob::$user at
/home/vagrant/projects/app/Jobs/RegisterConfirmationJob.php:37)
[stacktrace]
Why is this happening?
EDIT:
As requested, here is my entire RegisterConfirmationJob class:
<?php
namespace App\Jobs;
use App\Mail\RegisterConfirmation;
use App\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RegisterConfirmationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
}
It's simple, you hadn't declared $user as a class property, that is the error meaning.
I recommend you declare it as public property to use it at blade template
Yeah bro, it's not a Laravel problem, it's a PHP class definition problem, you can solve this by declaring the $user property, like that:
<?php
namespace App\Jobs;
use App\Mail\RegisterConfirmation;
use App\User;
use Illuminate\Support\Facades\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class RegisterConfirmationJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user; // you forgot of put this line
public function __construct(User $user)
{
$this->user = $user;
}
public function handle()
{
$user = $this->user;
Mail::to($user)->send(new RegisterConfirmation($user));
}
}
I am trying to start the work on broadcasting and event, but so far the event won't fire at all, and I don't have a clue what is going on
In my .env file I have this:
BROADCAST_DRIVER=log
Event class:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class OrdersStatusUpdate 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('orders');
// return new PrivateChannel('channel-name');
}
}
My controller:
<?php
namespace App\Http\Controllers\Frontend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Events\OrdersStatusUpdate;
class SiteController extends Controller
{
public function index(){
OrdersStatusUpdate::dispatch();
}
}
Nothing is happening when I fire a request in the browser (log is not created).
I am running PHP 7.4.3 on localhost:9099
What am I doing wrong here?
Try to run php artisan queue:work to start your queue worker and processing queue jobs. You can also use supervisor to automate this.
My mail laravel like this :
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class OrderReceivedMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
public $order;
public $store;
public function __construct($order, $store)
{
$this->order = $order;
$this->store = $store;
$this->subject('subject');
}
public function build()
{
$company_email = explode(',',config('app.mail_company'));
return $this->view('vendor.notifications.mail.email-order-received',['number'=>$this->order->number, 'store_name' => $this->store->name])->bcc($company_email);
}
}
My env like this :
BROADCAST_DRIVER=pusher
CACHE_DRIVER=redis
SESSION_DRIVER=file
QUEUE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
If the code executed, it does not works. The email failed sended or the mail code not executed
But if I remove implements ShouldQueue, it works. The email success sended
Why if I use shouldqueue it does not works?
When using ShouldQueue your emails are not sent instantly. You need to run the queue worker php artisan queue:work as described here https://laravel.com/docs/5.5/queues#running-the-queue-worker