Laravel - Broacasting with pusher - Invalid Signature - laravel-5

I just started using pusher because I had problems with redis and socket.io. For that I followed the documentation of Laravel, I created an account on pusher and insert the keys in the file .env.
BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=database
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
PUSHER_APP_ID=XXXXX
PUSHER_APP_KEY=XXXXXXXXXXXXX
PUSHER_APP_SECRET=XXXXXXXXXXXXXX
PUSHER_APP_CLUSTER=eu
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Then I created an event that implements the should broadcast interface
<?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 IncrementAddingOrderCounter implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $length;
public $value;
/**
* Create a new event instance.
*
* #param $length
* #param $value
*/
public function __construct($length, $value)
{
$this->length = $length;
$this->value = $value;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('add.order');
}
public function broadcastAs()
{
return 'increment.order';
}
}
Afterwards I emit the event with
event(new IncrementAddingOrderCounter(count($parsedData), $size));
But the execution of the event failed
and if I look in my logs I see this error
[2018-07-26 04:21:26] local.ERROR: Invalid signature: you should have sent HmacSHA256Hex("POST\n/apps/xxxxxx/events\nauth_key=xxxxxxxxxx&auth_timestamp=xxxxxxxx&auth_version=1.0&body_md5=xxxxxxxxxxxx", your_secret_key), but you sent "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
{"exception":"[object] (Illuminate\\Broadcasting\\BroadcastException(code: 0): Invalid signature: you should have sent HmacSHA256Hex(\"POST\
/apps/567300/events\
auth_key=xxxxxxxxxx&auth_timestamp=xxxxxxx&auth_version=1.0&body_md5=xxxxxxxxxxxx\", your_secret_key), but you sent \"xxxxxxxxxxxxxxx\"
at /home/oza/lab/php/Client1_GestionCommerce/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/PusherBroadcaster.php:116)
[stacktrace]
Does anyone have a solution on that because I search on the internet and I can not find anything

If you're sending the wrong signature, that usually means they key/secret/app id/cluster are not configured correctly. Can you double check these? You also potentially published a sensitive key in your post - I recommend obscuring it and generating a new id/key/secret combo (can be done from the Pusher dashboard!).

Related

How to solve Laravel Pusher Error data content of this event exceeds

I did the update to Laravel 9 today. Before, Pusher worked well on the application.
Now, I receive always the following error:
Pusher error: The data content of this event exceeds the allowed maximum (10240 bytes)
I didn't change the content.
I tested to change the content with the given supplier_id, with a fake id like "ee" and without any restrictions.
In the console log, I have the following result:
Why does pusher work, even there is an error in the backend?
<?php
namespace App\Events\Kanban;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class KanbanOrderCreatedEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $kanbanOrder;
public function __construct($kanbanOrder)
{
$this->kanbanOrder = $kanbanOrder;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return ['kanbanOrders.'.$this->kanbanOrder->network_supplier_id];
}
public function broadcastAs()
{
return 'kanbanOrderCreated';
}
public function broadcastWith()
{
return ['supplier_id' => $this->kanbanOrder->network_supplier_id];
}
}

Can't receive events broadcasted on Laravel

I've been struggling for a while now and can't find a way to make this work.
My project is on Laravel 8
I'm using Redis and laravel-echo-server (since it doesn't seem maintained I also tried with a hand-made express/socket.io server)
The problem : When I fire the event, it's received by Redis and Laravel Echo Server but it's never received on my webpage's console.
"Events/SendMessage"
<?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 SendMessage 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('user-channel');
}
public function broadcastAs()
{
return 'UserEvent';
}
public function broadcastWith()
{
return [
'title' => 'This notification is awesome'
];
}
}
".env"
BROADCAST_DRIVER=redis
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
LARAVEL_ECHO_PORT=6001
"welcome.blade.php"
<script>
window.laravel_echo_port='{{env("LARAVEL_ECHO_PORT")}}';
</script>
<script src="https://cdn.socket.io/3.1.3/socket.io.min.js" integrity="sha384-cPwlPLvBTa3sKAgddT6krw0cJat7egBga3DJepJyrLl4Q9/5WLra3rrnMcyTyOnh" crossorigin="anonymous"></script>
<script src="{{ ('/js/laravel-echo-setup.js') }}" type="text/javascript"></script>
<script>
window.Echo.channel('user-channel').listen('.SendMessage', (e) => {
console.log('Got event...');
console.log(e);
});
</script>
"laravel-echo-setup.js"
import Echo from 'laravel-echo';
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ":6001"
});
Hope someone can help me
If you need anymore code, I'm here !!

Send Grid send email failed on localhost in laravel 5.7.29

Im trying to send email from my local host using sendgrid, packages s-ichikawa 2.1.0 , laravel 5.7.29, Im getting this Error
Client error: POST https://api.sendgrid.com/v3/mail/send resulted in a 403 Forbidden response: {"errors":[{"message":"The from address does not match a verified Sender Identity. Mail cannot be sent until this error (truncated...) *
Same code working on other project in API , but on localhost Im this error.
MY Controller Code:
$email = 'umermajeed93#hotmail.com';
$name = 'Umer Majeed';
Mail::to($email)->send(new SendMailable($name ,$email))
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use Sichikawa\LaravelSendgridDriver\SendGrid;
class SendMailable extends Mailable
{
use Queueable, SerializesModels;
use SendGrid;
public $name;
public $code;
public $email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($name,$email)
{
$this->name = $name;
$this->email = $email;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$address = 'noreply#retailplatform.com';
$subject = 'New Ad';
return $this
->view('Admin.email')
->subject($subject)
->from($address)
->to([$this->email])
->sendgrid([
'personalizations' => [
[
'substitutions' => [
':myname' => 's-ichikawa',
],
],
],
])->with('name',$this->name);
}
}
in the test environment only emails are sent to your test account, which you set up in the .env file:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=
MAIL_PASSWORD=
in production you could already send emails to the recipient of your choice, but on the local server you cannot

Why queue still waiting when send mail in laravel?

i want send mail in queue and have not waiting when send mail
https://laravel.com/docs/5.7/queues#connections-vs-queues
i run command to create table jobs:
php artisan queue:table
php artisan migrate
I create a job to send mail: php artisan make:job SendEmailJob
and edit code :
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Mail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* #return void
*/
public $body;
public $emailto;
public function __construct($body,$email)
{
//
$this->body=$body;
$this->emailto=$email;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email=$this->emailto;
Mail::send("body_email.confirm_order",['Body'=> $this->body], function($message) use ($email)
{
$message->from(env('MAIL_USERNAME'),"Eye glasses");
$message->subject("Confirm Email");
$message->to($email);
});
}
}
I call queue from controller:
use App\Jobs\SendEmailJob;
public function index()
{
$Body="test";
$email="daitb#vnitsolutions.com";
SendEmailJob::dispatch($Body, $email);
$calendars= AppointmentModel::GetAppointmentofDoctor($id,$datetime);
return view('frontend.appointment',["calendars"=>$calendars]);
}
add QUEUE_DRIVER=database to file .env
run command:
php artisan queue:work
If i run controller, process still waiting send mail finish and run other process.
i try change to:
SendEmailJob::dispatch($Body, $email)->delay(now()->addMinutes(3));
It not delay,it still send mail after 5s.
Why queue still waiting when send mail in laravel?
I using win 32.
My Problem fixed by change QUEUE_CONNECTION=sync to QUEUE_CONNECTION=database in .env file

Keep PHPMailler connection alive over Laravel's queue

I want to send many emails.
Currently I write basic code use PHPMailler to send mail using queue. It works, but everytime new queue is run, it have to connect to SMTP again, so i get bad perfomance.
I find SMTPKeepAlive property on PHPMailler documentation:
$phpMailer = New PHPMailer();
$phpMailer->SMTPKeepAlive = true;
Is it imposible and how to keep $phpMailler object for next queue? So PHPMailler have not to connect again by using previous connection.
If you are using Laravel then you have to use Laravel's feature inbuilt.
Please find below documents:
https://laravel.com/docs/5.6/mail
Please find a piece of code for send mail and adding in a queue:
use App\Mail\EmailVerifyMail;
\Mail::queue(new EmailVerifyMail($users));
EmailVerifyMail.php
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class EmailVerifyMail extends Mailable
{
use Queueable, SerializesModels;
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$this->to($this->user)->subject(__('mail.subjects.verification_link', ['USERNAME' => $this->user->name]));
return $this->view('mails/emailVerify', ['user' => $this->user]);
}
}

Resources