Handling Active MQ custom messages Laravel - laravel-5

I have been struggling with handling ActiveMQ custom messages push.
I have a 3rd party software that pushes messages into ActiveMQ queue.
what should be the message structure like?
how can i read the message body in my job?

You can Enqueue like this
$sMessage = json_encode(
array(
'job' => 'path/to/your/job/file',
'data' => $aDataToSend,
'queue' => /queue/name/to/append,
'attempts' => 1
)
);
And can consume like this:
class JobClassName extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels, BaseJob;
public function __construct()
{}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{}
public function fire($oJob, $aData)
{}
}

Related

Broadcast event to Pusher when database notification is created

This is related to an earlier question I asked: Am I overcomplicating events/listeners/notifications?.
Based on the feedback received on that question, I modified my approach to attempt to do the following:
Fire a database notification.
Listen for a new database notification to be created
Fire an event and broadcast that to pusher.
So first I send the database notification:
Notification::send($this->requestedBy, new SendMonthlySummaryCreatedNotification($fileDownloadUrl, $fileName));
That notification class looks like this:
class SendMonthlySummaryCreatedNotification extends Notification implements ShouldQueue
{
use Queueable;
public $fileDownloadUrl;
public $fileName;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct($fileDownloadUrl, $fileName)
{
$this->fileDownloadUrl = $fileDownloadUrl;
$this->fileName = $fileName;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database'];
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'title' => 'Monthly Summary Complete',
'message' => "{$this->fileName} is ready. ",
'link' => $this->fileDownloadUrl,
'link_text' => 'Click here to download',
'show_toast' => true,
'user_id' => $notifiable->id
];
}
}
I found this example of how to add a $dispatchesEvents property to a model in the docs, which I modified to apply it to a new model I created that extends the DatabaseNotification class, which I learned about on this SO question.
class Notification extends DatabaseNotification
{
use HasFactory;
protected $dispatchesEvents = [
'created' => NotificationCreatedEvent::class
];
public function users()
{
return $this->belongsTo(User::class, 'notifiable_id');
}
}
Theoretically the above should dispatch an event when my notification is sent, and then I have the NotificationCreatedEvent that I'd like to use to broadcast to pusher:
class NotificationCreatedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
protected $notification;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Notification $notification)
{
$this->notification = $notification;
Log::debug($this->notification);
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('users.' . $this->notification->notifiable_id);
}
}
The problem is that everything is working up until the NotificationCreatedEvent It doesn't seem to be getting fired. I don't know if I need to do anything in addition to mapping it in my new Notification model, or if it should just work.
My goal is to add a database notification, and then whenever that happens to send it to pusher so I can notify the user in real-time. This seems like it should work, but I'm not seeing anything coming over in pusher.

Laravel Echo with Pusher and Livewire - not receiving notifications client-side

I'm able to broadcast a notification to Pusher, but I'm unable to receive the response back in my livewire component.
Here is my Notification class:
<?php
namespace App\Notifications;
use App\Models\Statement;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class StatementCompletedNotification extends Notification implements ShouldQueue, ShouldBroadcast
{
use Queueable;
public $statement;
/**
* Create a new notification instance.
*
* #return void
*/
public function __construct(Statement $statement)
{
$this->statement = $statement;
}
/**
* Get the notification's delivery channels.
*
* #param mixed $notifiable
* #return array
*/
public function via($notifiable)
{
return ['database', 'broadcast'];
}
/**
* Get the array representation of the notification.
*
* #param mixed $notifiable
* #return array
*/
public function toArray($notifiable)
{
return [
'user_id' => $this->statement->uploadedBy->id,
'statement_id' => $this->statement->id,
'file_name' => $this->statement->original_file_name
];
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('users.' . $this->statement->uploadedBy->id);
}
}
And here is the getListeners() method on my Livewire component. I've tried several different things here, first off I tried the way it's shown in the docs, just by referencing my StatementCompletedNotification in the listener, like so:
public function getListeners()
{
return [
"echo-private:users.{$this->user->id},StatementCompletedNotification" => 'refreshNotifications'
];
}
I noticed that in pusher, my event type is listed as Illuminate\Notifications\Events\BroadcastNotificationCreated, and I found this post online, so I tried that method like so:
public function getListeners()
{
return [
"echo-private:users.{$this->user->id},.Illuminate\\Notifications\\Events\\BroadcastNotificationCreated" => 'refreshNotifications'
];
}
Neither way has worked for me.
Here's where I'm attempting to just get something back in my javascript on the client-side:
Echo.private('users.1')
.notification((notification) => {
console.log(notification);
});
I don't get any response and I have no idea what the problem is. I've spent an insane amount of time on this and can't seem to figure it out.
Also, it appears that my notification is being picked up multiple times in the queue:
A little more background, the flow that I have set up right now is basically:
StatementCompleted event gets fired (not queued), there's a listener that handles the StatementCompleted event which is queued, and calls my StatementCompletedNotification class like so:
public function handle($event)
{
$event->statement->uploadedBy->notify(new StatementCompletedNotification($event->statement));
}

Laravel queue email with attachment

Hi everyone i'm just trying to queue my email with attachment in laravel project, but it does not seems to work. when i try to send an attachment without queue my mail it is working absolutely fine, but when i put my email in queue it just stop working. email is sent but attachment doesn't.
here is my code for email in controller
Mail::to($request->email)->send(new ContactUsEmail($contact_us));
and below code is from my mail job
class ContactUsEmail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
/**
* Create a new message instance.
*
* #return void
*/
protected $data;
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$extension = explode('.',$this->data->attachment)[1];
return $this->subject($this->data->topic)->attach(public_path('/storage/attachments/contact-us/' . $this->data->attachment),
[
'as' => $this->data->attachment,
'mime' => 'application/'. $extension
])->markdown('mail.contact-us' ,[
'subject' => $this->data->subject,
'name' => $this->data->name,
'email' => $this->data->email,
'description' => $this->data->description,
]);
}
}
please let me know if i'm doing something wrong or missing something.
i appreciate your response

How can I send an email to many recipients in laravel?

I can send emails to a lot of recipients but the problem is I am using a business email which I'm subscribe to my web host, the problem is when I send 1 email to a lot of recipients my web host automatically suspends my email's outgoing which technically I'm having problems with right now, so I tried using mailing list but it won't send emails to my recipients using the Mail(), but it works when sending a mail to mailist-join#domain.com.
Controller:
public function imail($request){
$dataEmail = [
'date' => $request->date,
'time_start' => $request->start,
'time_end' => $request->end,
'duration' => abs(strtotime($request->end) - strtotime($request->start))/(60*60),
'areas' => Purifier::clean($request->areas),
'reason' => Purifier::clean($request->activities)
];
$emails = UserEmail::where('email','!=','')->select('email')->get()->pluck('email');
$subject = 'ADVISORY (' . date("F j, Y",strtotime($request->date)) .')';
foreach ($emails as $email) {
Mail::to($email)
->send(new SendToAll($dataEmail,$subject));
}
}
SendToAll Mail
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendToAll extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;
// public $afterCommit = true;
public $data,$subject;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data,$subject)
{
$this->data = $data;
$this->subject = $subject;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject($this->subject)
->from('myemail#domain.com','Me')
->view('pages.imail')
->with('data',$this->data);
;
}
}
I also tried supervisor but to no avail. I'm using Windows Server 2012.
You have to use like below code
Mail::to($email)->cc(['mail1','mail2','mail3'])->send(new SendToAll($dataEmail,$subject));

Queue Job not saving to Tenant database and not using Tenant SMTP

I have a multi-tenant setup which allows each tenant to save their own SMTP information in a settings table. The job being saved right now is being sent to the system database instead of the tenant. This is causing an issue as a Service Provider is setup to configure the mail configuration on boot() to each of the provided values in the database. With the worker running the email job does send but uses the stored system SMTP values instead of the tenants - which I assume is just because it's looking in the same database as the current job's connection.
Service Provider
class TenantEmail extends ServiceProvider
{
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
Config::set('mail.host', setting('admin.email_host'));
Config::set('mail.port', setting('admin.email_port'));
Config::set('mail.encryption', setting('admin.email_encrypt'));
Config::set('mail.username', setting('admin.email_username'));
Config::set('mail.password', setting('admin.email_password'));
}
}
SendEmail Job
Looking over the documentation on Hyn is looks like I can force set the tenant website Id during the dispatch but that didn't make a difference.
class SendEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $user;
public $sub;
public $content;
public $unsubscribeUrl;
public $replyAddress;
public $website_id;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($row, $subject, $message, $unsubscribeUrl, $replyTo,int $website_id)
{
//
$this->user = $row;
$this->sub = $subject;
$this->content = $message;
$this->unsubscribeUrl = $unsubscribeUrl;
$this->replyAddress = $replyTo;
$this->website_id = $website_id;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
//
$email = new ConnectEmail($this->user, $this->sub, $this->content, $this->unsubscribeUrl, $this->replyAddress);
Mail::to($this->user->email_address, $this->user->name)->send($email);
}
}
Not the cleanest approach perhaps but I was able to solve this by passing the SMTP information in my email controller when I dispatch the job.
$smtp = [
'host' => setting('admin.email_host'),
'port' => setting('admin.email_port'),
'encryption' => setting('admin.email_encrypt'),
'username' => setting('admin.email_username'),
'password' => setting('admin.email_password')
];
$emailJob = (new SendEmail($row, $subject, $message, $unsubscribeUrl, $replyTo, $websiteId, $smtp));
dispatch($emailJob)->delay($scheduleSend);
Then in my SendEmail job I just include the $smtp in the ConnectEmail and pass that over to the mailable, which in the init construct I just add in:
if(!empty($smtp)){
Config::set('mail.host', $smtp['host']);
Config::set('mail.port', $smtp['port']);
Config::set('mail.encryption', $smtp['encryption']);
Config::set('mail.username', $smtp['username']);
Config::set('mail.password', $smtp['password']);
}
This then queues up and sends the job using the dynamic SMTP information without a problem.

Resources