Mohsen
Mohsen

Reputation: 292

Why pusher does not work in laravel echo on localhost?

I want the status to be live when the user logs in and logs out. I get the connection to the pusher and everything is registered but the javascript side doesn't work (STEP 3) and it doesn't show anything in console.log. I get status 101

These are the steps I took:

"pusher/pusher-php-server": "^7.2",
"laravel-echo": "^1.16.0",
"pusher-js": "^8.4.0-rc2",
  1. adding the keys to .env file
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=098576747
PUSHER_APP_KEY=34kkh4jhj56mn7nl7h5
PUSHER_APP_SECRET=h4857jfhf83746565
PUSHER_APP_CLUSTER=eu

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
  1. bootstrap.js
window._ = require('lodash');
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.$ = require('jquery');

import Echo from 'laravel-echo';

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    encrypted: true,
    forceTLS: true
});
  1. app.js
Echo.channel('notifications')
    .listen('LogInLogOutEvent', (e) => {
        console.log(e)
    })
  1. laravel.log

enter image description here

  1. broadcasting.php
'pusher' => [
            'driver' => 'pusher',
            'key' => env('PUSHER_APP_KEY'),
            'secret' => env('PUSHER_APP_SECRET'),
            'app_id' => env('PUSHER_APP_ID'),
            'options' => [
                'cluster' => env('PUSHER_APP_CLUSTER'),
                'useTLS' => true,
                'encrypted' => true,
            ],
        ],

  1. the event
class LogInLogOutEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $message;
    public $type;

    /**
     * Create a new event instance.
     */
    public function __construct($message, $type)
    {
        //
        $this->message = $message;
        $this->type = $type;
    }


    public function broadcastOn()
    {
        Log::debug($this->message);
        Log::debug($this->type);
        return new Channel('notifications');
    }
}

  1. Listeners
   public function handle(Login $event): void
    {
        broadcast(new LogInLogOutEvent("{$event->user->name} is online", 'success'));
    }

    public function handle(Logout $event): void
    {
        broadcast(new LogInLogOutEvent("{$event->user->name} is offline", 'danger'));

    }


  1. EventServiceProvider.php
 Login::class => [
            LogInListener::class,
        ],
        Logout::class => [
            LogOutListener::class,
        ],

enter image description here

10. enter image description here

enter image description here

Upvotes: 0

Views: 259

Answers (0)

Related Questions