Event broadcast is not working when queueis use in our project - laravel-5

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

Related

Laravel-echo-server with Socket.io not detecting file changes

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?

Database listening with Laravel Websocket

I have done every installation related to Laravel Websocket. Here's what I'm trying to do: I have a table in the database where I keep my orders. When a new order arrives, I want to print it to the console on my home page blade.
I would be glad if you help. Thanks.
You can use model event like below, in your Order model
protected static function booted()
{
//everytime when new order is created this function will be called
static::created(function ($order) {
//then broadcast event
NewOrder::dispatch($order);
});
}
Then you have to define channel in youre route/channel.php
use App\Models\Order;
Broadcast::channel('newOrder', function ($user) {
return $user->id;
});
Then create event php artisan make:event NewOrder
class NewOrder implements ShouldBroadcast
{
/**
* The user that created the server.
*
* #var \App\Models\User
*/
public $user;
/**
* Create a new event instance.
*
* #param \App\Models\User $user
* #return void
*/
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* #return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('newOrder');
}
}
then listen for NewOrder event in newOrder Channel
Echo.channel(`newOrder`)
.listen('NewOrder', (e) => {
console.log(e.order);
});
Read Documentation more info https://laravel.com/docs/9.x/broadcasting#pusher-channels

How to Trigger an event in Laravel pusher

I have an issue with Laravel Pusher events
first of all
when I fire the event from the pusher debug console it works fine
But when I try to do it from my laravel controllers, it doesn't.
and here is my code
my event
class FileAdded 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('file.add');
}
public function broadcastAs()
{
return 'my-event';
}
}
my pusher client side
Pusher.logToConsole = true;
var pusher = new Pusher('xxxxxxxxxxx', {
cluster: 'eu'
});
var channel = pusher.subscribe('file.add');
channel.bind('my-event', function(data) {
alert('hi');
});
where I'm -supposedly- triggering my event
public function store(Request $request)
{
event(new FileAdded());
//some unrelated stuff happen in that function
}
and here is my console
Pusher : : ["State changed","initialized -> connecting"]
pusher.min.js:8 Pusher : :
["Connecting",{"transport":"ws","url":"wss://ws-eu.pusher.com:443/app/b7f91be243251ea13075?protocol=7&client=js&version=7.0.3&flash=false"}]
pusher.min.js:8 Pusher : : ["State changed","connecting -> connected
with new socket ID 131587.15868131"] pusher.min.js:8 Pusher : :
["Event
sent",{"event":"pusher:subscribe","data":{"auth":"","channel":"file.add"}}]
pusher.min.js:8 Pusher : : ["Event
recd",{"event":"pusher_internal:subscription_succeeded","channel":"file.add","data":{}}]
pusher.min.js:8 Pusher : : ["No callbacks on file.add for
pusher:subscription_succeeded"]
So what could be the problem and thanks in advance!

Why I cannot send messages to pusher but I can receive them from pusher?

Im making a chat in my app and Im using pusher. I did everything that pusher told me to do. I'm using vanilla-js in frontend. I can connect in frontend. I can receiver messages from pusher (look at channel.bind()) but my messages are not going anywhere.
P.S I did all configuration (.env, broadcasting.php)
FRONTEND
var pusher = new Pusher('xxxxxxxx', {
cluster: 'eu',
forceTLS: false
});
window.channelName = 'my-channel-' + {{ $chat->id }};
window.event = 'private-conversation';
var channel = pusher.subscribe(window.channelName);
channel.bind(window.event, function (data) {
alert(data);
});
BACKEND
public function sendMessage($request, $roomId){
event(new Event(array(
'id' => auth()->user()->id,
'full_name' => auth()->user()->fullName()
), $request->input('channel'), $request->input('event'), $request->input('message')));
}
EVENT
class Event
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
public $channelName;
public $event;
/**
* Create a new event instance.
*
* #return void
*/
public function __construct($user, $channelName, $event, $message)
{
$this->user = $user;
$this->channelName = $channelName;
$this->event = $event;
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* #return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return [$this->channelName];
}
public function broadcastAs()
{
return $this->event;
}
}

Laravel 5.2 not broadcasting events

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.

Resources