I have a very simple application that uses Laravel-echo-server with Socket.io for real-time communication, the application is working, the problem is that when I change the message of the event that is going to be executed, it completely ignores all the changes of the php file, for example I have the following class.
class PublicMessage 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('public-message-channel');
}
public function broadcastAs()
{
return 'MessageEvent';
}
public function broadcastWith()
{
return ['message' => "This is a public message"];
}
}
I subscribe as follows in my front end
window.Echo.channel('public-message-channel').listen('.MessageEvent', (data) => {
console.log(data);
});
then i trigger the event from a route
Route::get('/chat', function(){
event(new PublicMessage());
dd("Public event exceute successful.");
});
and i get
{"message": "This is a public message"}
but if i change the return in broadcastWith function i still get the same response, even restarting the php artisan queue:work the laravel-echo-server and my Xampp server, the only thing that makes it detect the changes is restarting the machine I'm developing on, does anyone have an idea what could be going on?
Related
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));
}
I am using Pusher 5 running on Laravel 8, I was trying to broadcast user verified event. However, when I verify my account I get the following error:
Pusher error
My listener code is as follows:
<?php
namespace Illuminate\Auth\Events;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Broadcasting\Channel;
class Verified implements ShouldBroadcast
{
use SerializesModels;
public $afterCommit = true;
/**
* The verified user.
*
* #var \Illuminate\Contracts\Auth\MustVerifyEmail
*/
public $user;
/**
* Create a new event instance.
*
* #param \Illuminate\Contracts\Auth\MustVerifyEmail $user
* #return void
*/
public function __construct($user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new Channel('users');
}
public function broadcastAs()
{
return 'account-verified';
}
public function broadcastWith()
{
return ['id' => 'heya'];
}
}
Any help would be highly appreciated!
try composer require pusher/pusher-php-server ^4.1,
at least this works with the beyondecode/laravel-websockets,
not sure about the official pusher server
I am going through this tutorial Introducing Laravel Echo. Broadcasting thing is working perfecting fine. Whenever I execute the command php artisan chat:message "something". Event get triggered and data stores in database. But moving on to Laravel Echo. I tried a lot but I am going no where. I have put Echo in /js/app.js but in docs it mentioned that /js/bootstrap.js. Since i am following the tutorial, therefore I have put it in /js/app.js Echo is not showing data in log. I am using ubuntu.
Test Event:
class TestEvent implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $message;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($user, $message)
{
//
$this->user = $user;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
return "chat-room";
}
}
Send Chat Command Code:
class SendChatMessage extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'chat:message {message}';
protected $description = 'Send chat message.';
public function handle()
{
// Fire off an event, just randomly grabbing the first user for now
$user = \App\User::first();
$message = \App\Message::create([
'user_id' => $user->id,
'content' => $this->argument('message')
]);
event(new \App\Events\TestEvent($message, $user));
}
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
}
Laravel Echo:
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'my-key'
});
window.Echo.channel('chat-room')
.listen('TestEvent', (e) => {
console.log(e.user, e.message);
});
one more thing whenever I send an event from pusher. Pusher is able to send it correctly.
Not sure if it is still relevant but might help someone.
I am using Laravel 5.4 and using 'eu' cluster in pusher. Please note that sometimes pusher goes crazy. It starts receiving broadcasted events late. So, I made my event implement ShouldBroadcastNow contract instead of ShouldBroadcast to overcome this problem. I am not using a queue to post events to pusher. Even then sometimes I notice delays in receiving events. Anyways, all well on the broadcasting part.
Now, on the client side, here is the script I am using to listen to events triggered from pusher(in my case private channel)
bootstrap.js (available by default in Laravel 5.4)
import Echo from "laravel-echo"
window.Echo = new Echo({
broadcaster: 'pusher',
key: '<your_pusher_app_key>',
csrfToken: window.Laravel.csrfToken,
cluster:'eu',
encrypted: true });
You should compile the JS if you make change to bootstrap.js else, your changes won't be packed into public folder. Laravel 5.4 uses Laravel Mix and the command to compile is "npm run dev"
In My blade template:
window.Echo.private('<channel_name_without_prefixing_private_keyword>')
.listen("<Just the event class name and not fully qualified name>", function(e){
console.log(e);
});
This worked for me. Further, ensure that, BroadcastServiceProvider is uncommented in config\app.php file. It's not enabled by default.
In routes/channels.php
Broadcast::channel('<channel_name_without_private_prefix>', function ($user) {
//Auth check
return true;
});
I've tried to broadcast event with pusher. But after a long hours of debugging I am still without a working solution.
Pusher works great, it's getting events from the debug console. Laravel is also firing the event. I also set up the queue and broadcasting configuration, but I noticed that my queue listener never getting any response.
This is my event listener:
<?php
namespace App\Listeners;
use App\Events\SomeEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
class EventListener implements ShouldQueue
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* #param SomeEvent $event
* #return void
*/
public function handle(SomeEvent $event)
{
//dd($event);
}
}
Routes:
Route::get('event', function () {
event(new App\Events\SomeEvent());
return "event fired";
});
Event file:
<?php
namespace App\Events;
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class SomeEvent extends Event implements ShouldBroadcast
{
use SerializesModels;
public $data;
public $x = 1111;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct()
{
$this->data = array(
'power'=> '10'
);
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return ['test_channel'];
}
}
Edit:
i reinstalled laravel and now the queue seems to respond and create row in the job table. but the pusher still dont recive any events from laravel.
Your App\Providers\EventServiceProvider needs to be made aware of the events and listeners.
protected $listen = [
App\Events\SomeEvent::class => [
App\Listeners\SomeListener::class,
],
];
These can be automatically populated by running php artisan event:generate.
I was broadcasting my event with help of pusher,it's worked fine but when i used queue implementation then pusher haven't receive any broadcast or may be event is not broadcasting.I'm not understand what the issue is.Code is given below please help me
Controller function
public function index()
{ $this->user_id=2;
Event::fire(new UpdateDeviceStatus($this->user_id));
}
Event file
class UpdateDeviceStatus extends Event implements ShouldBroadcast
{
use SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public $devices;
public function __construct($id)
{
$this->devices=Device::with('units')->where('user_id',$id)->get();
}
/**
* Get the channels the event should be broadcast on.
*
* #return array
*/
public function broadcastOn()
{
return ['update-status'];
}
}
js file
Pusher.logToConsole = true;
var pusher = new Pusher('key', {
encrypted: true
});
var channel = pusher.subscribe('update-status');
channel.bind('App\\Events\\UpdateDeviceStatus', function (data) {
console.log(data);
});
I had the same issue and realised that I just forgot to listen to the queue: php artisan queue:listen redis