I was trying to catch a fired event on my Web sockets Dashboard but my event is returning [NULL] when fired.
Please note that it is only happening in my existing project. I tried doing the same on a fresh Laravel project and it is working just fine. The event returns [] (blank array) when fired and the content of the event gets shown in the Laravel Web sockets dashboard.
Any idea on why it is returning NULL on my existing project? What can possibly be wrong.
Below is my Event file (RealTimeMessage.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 RealTimeMessage implements ShouldBroadcast
{
use SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct()
{
$this->message = "Hello World";
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('custom-event');
}
}
Related
I have created a feature called test to test the admin registration. When i run the testcases it gives me this error
1) Tests\Feature\Admin\RegisterTest::testValidRegistration
The expected [App\Events\UserRegistered] event was not dispatched.
Failed asserting that false is true.
/var/www/html/gas-backend/tests/Feature/Admin/RegisterTest.php:85
Below i have stated the admin registration test file and user registered event file
Admin/RegisterTest.php
$response->assertStatus(201);
$response->assertJson($expectedResponse);
$this->assertDatabaseHas('users', $expectedDatabaseRecord);
Event::assertDispatched(UserRegistered::class);
Notification::assertNotSentTo($user, RegistrationComplete::class);
UserRegistered.php
<?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;
use App\Models\User;
class UserRegistered
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
//return new PrivateChannel('channel-name');
}
}
How should i resolve this error ?
I am trying to implement a private channel for the first time on Laravel and VueJS. I have gotten to the point where the event triggers as expected, but I cannot listen to it in the component that I want it to.
I followed all the steps of installing the appropriate dependencies. Can someone please tell me why this might be?
My listener:
Echo.private('message')
.listen('NewTeam', (e) => {
console.log('made it');
});
My event:
namespace App\Events;
use App\Team;
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;
use Illuminate\Support\Facades\Log;
class NewTeam implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* #return void
*/
public $team;
public function __construct(Team $team)
{
$this->team = $team;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('message');
}
public function broadcastWith()
{
return ["message" => 'A new team has arrived'];
}
My channel.php:
Broadcast::channel('message', function ($user) {
return true;
});
My pusher account tells me that it is sending. However, when I trigger the event, I do not receive anything from the listener.
It might be the event name that you are using. In your listener you are listening the "NewTeam" event on the message channel.
Echo.private('message')
.listen('NewTeam', (e) => { // <---
console.log('made it');
});
But in your event you aren't specifying a custom event name. According to the docs:
Broadcast Name
By default, Laravel will broadcast the event using the event's class
name. However, you may customize the broadcast name by defining a
broadcastAs method on the event:
/**
* The event's broadcast name.
*
* #return string
*/
public function broadcastAs()
{
return 'server.created';
}
So this means that the event name used in your case propably is App\\Events\\NewTeam. In order to address/custom this the way you want, you'll need to add to your event class:
app/Events/NewTeam.php
/**
* The event's broadcast name.
*
* #return string
*/
public function broadcastAs()
{
return 'NewTeam';
}
Using Pusher with Laravel 5.8 to send messages in real-time generates this error on NewMessage Event file.
Steps I took to try and debug:
tried removing the line "use Dispatchable, InteractsWithSockets, SerializesModels;" inside the Class;
tried without using the Classes at the top of the file
none of those worked. On Laravel 5.8 Documentation they don't mention using this line inside our Event Classes, maybe it's outdated?!
File looks like this:
`
namespace StyxEminus\Events;
use StyxEminus\Message;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct(Message $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('messages.' . $this->message->to);
}
public function broadcastWith() {
return ["message" => $this->message];
}
}`
Local Server: Apache on Xampp;
Operating System: W10 64bit
Browser: Brave(chromium) & Chrome
you need import Illuminate\Foundation\Bus\Dispatchable
Not sure why I am having this issue, it should be a simple utilization of the use statement as is frequently done in Laravel controllers and repositories.
Is there something different for event listeners?
My error is:
Class 'App\Listeners\Asset' not found
It fires from my event listener:
<?php
namespace App\Listeners;
use App\Events\FFMPEGcreateAVideo;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\Models\Asset;
use Auth;
use Illuminate\Support\Facades\Log;
class FFMPEGcreateAVideoListener implements ShouldQueue
{
/**
* Create the event listener.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* #param FFMPEGcreateAVideo $event
* #return void
*/
public function handle(FFMPEGcreateAVideo $event)
{
$assetID = $event->assetID;
$assetURL = $event->assetURL;
$newAssetURL = preg_replace('/(.*\.(?!.*\.))(.*)/','${1}mp4',$assetURL);
$coverPhotoPath = $event->coverphoto;
$asset = Asset::where("id",$assetID)->first(); //error here
Edit: I've tried both composer dump-autoload and php artisan cache:clear.
Furthermore I know it is this listener.
The error in full is:
[2017-12-31 00:05:12] local.ERROR:
Symfony\Component\Debug\Exception\FatalThrowableError: Class
'App\Listeners\Asset' not found in
/var/www/html/app/Listeners/FFMPEGcreateAVideoListener.php:35 Stack
trace
So, I am trying to make use of the broadcasting functionality in laravel to broadcast notifications on certain events.
<?php
namespace Uppdragshuset\AO\Tenant\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class ImportSuccess implements ShouldBroadcast
{
use InteractsWithSockets, SerializesModels;
public $job_id;
public $patent_id;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($job_id, $patent_id)
{
$this->job_id = $job_id;
$this->patent_id = $patent_id;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
dd(5);
return [];
}
}
I have tried dying and dumping even tried logging using the Log facade but the broadcastOn method is not firing. I am not sure what I am doing wrong here? I am coming from an old version of laravel and updated it recently so I installed the BroadcastServiceProvider manually and registered it in the app.php too. So what am I missing?
I can use Pusher class to fire using the Pusher class but not via broadcasting.