I used pusher for my project. I configure broadcasting as per laravel docs. When I fired my event pusher does not work for me. But when I send data from pusher console then pusher receive this data. I also try vinkla/pusher. Its work fine but laravel event broadcasting not work. Please help me.
Here is my TestEvent.php
code
namespace Factum\Events;
use Factum\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TestEvent implements ShouldBroadcast
{
use SerializesModels;
public $text;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($text)
{
$this->text = $text;
}
/**
* Get the channels the event should be broadcast on.
*
* @return array
*/
public function broadcastOn()
{
return ['test-channel'];
}
}
I encountered a similar problem and went step by step and fixed the issues. I will assume you are running Laravel 5.3. Here is a step by step walkthrough that might be helpful:
Check your config file. In config\broadcasting.php
:
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_KEY'),
'secret' => env('PUSHER_SECRET'),
'app_id' => env('PUSHER_ID'),
'options' => [
'cluster' => 'eu',
'encrypted' => true,
// 'host' => 'api-eu.pusher.com'
// 'debug' => true,
],
],
],
Create a route for testing in your web.php
file:
Route::get('/broadcast', function() {
event(new \Factum\Events\TestEvent('Sent from my Laravel application'));
return 'ok';
});
In your TestEvent.php
event file you should add this method in order to specify your event name:
/**
* The event's broadcast name.
*
* @return string
*/
public function broadcastAs()
{
return 'my_event';
}
Open your Pusher dashboard and go to debug console. Keep the page open so you can notice if you got a successful request from your application.
Start or restart your queue worker. This step can make or break everything. If you are using Mysql tables for queues I will assume that you already set up your "jobs" and "failed_jobs" database tables necessary for the queues. Another important element is the worker - the queue processor. Without an active worker running to process your queue, the jobs (TestEvent) will "remain" in the jobs table, meaning the jobs are pending and nothing will happen further until an active worker starts processing the queue.
You can start the worker like this:
www@yourmachine# PHP artisan queue:work --tries=3
Now that you have everything in order to make a call on "http://your-app.laravel/broadcast" and check your pusher debug console for a response.
Optional step: If something is still missing, you can debug your app interaction with Pusher like so: In your testing route try doing this:
Route::get('/broadcast', function() {
// New Pusher instance with our config data
$pusher = new \Pusher(
config('broadcasting.connections.pusher.key'),
config('broadcasting.connections.pusher.secret'),
config('broadcasting.connections.pusher.app_id'),
config('broadcasting.connections.pusher.options')
);
// Enable pusher logging - I used an anonymous class and the Monolog
$pusher->set_logger(new class {
public function log($msg)
{
\Log::info($msg);
}
});
// Your data that you would like to send to Pusher
$data = ['text' => 'hello world from Laravel 5.3'];
// Sending the data to channel: "test_channel" with "my_event" event
$pusher->trigger( 'test_channel', 'my_event', $data);
return 'ok';
});
I hope it will work for you too! Have a good time coding! ;)