15

Problem

I can confirm that Pusher API is receiving the message. I saw in Debug console of Pusher website. But listen callback is not working at all.

I am following this tutorial to implement Pusher in Laravel 5.4

Below were the step by step things done.

  1. composer require pusher/pusher-php-server
  2. npm install --save laravel-echo pusher-js
  3. instantiated the Echo instance in your resources/assets/js/bootstrap.js
  4. Initialized the pusher key in env and in bootstrap.js file.

Finally, I wrote below code in blade.

<script>
    window.Echo.channel('SendMessageChannel.1')
        .listen('App.Events.SendMessageEvent', (e) => {
            console.log(e);
        });
</script>

Controller Code

broadcast(new SendMessageEvent("Hi"))->toOthers();

Event Code

class SendMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $Message;

    public function __construct($message)
    {
        $this->Message = $message;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('SendMessageChannel.2');
    }

}

Am I missing anything?

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • You don't want to completely replace the SessionGuard. Passport should only cover your API routes. SessionGuard shold cover any route that returns HTML (web routes). Then any javascript calls should use your API as explained in the ["Consuming Your API With JavaScript"](https://laravel.com/docs/5.4/passport#consuming-your-api-with-javascript) section of the documentation – jfadich Mar 29 '17 at 19:48
  • Hi Pankaj, did you get solution of the issue? – Himanshu Upadhyay Jul 30 '17 at 06:07
  • Not yet, waiting for a solution. – Pankaj Jul 30 '17 at 06:42
  • what is the output of your chrome console of the WS-connection? maybe you are not authorized – cre8 Jul 30 '17 at 10:21
  • I am using echo.js for laravel broadcasting. There is no console message coming on sending the message. But the message is going to pusher console. Interested in viewing it in Team viewer? – Pankaj Jul 30 '17 at 10:29

4 Answers4

6
  1. You have to listen to: SendMessageEvent without the namespace.
  2. when you listen to a private channel, you need to to listen to private-SendmessageChannel or you use Echo.private('SendmessageChannel')

Because we fixxed the issue via teamspeak at some parts it's difficult to explain it in this answer in full detail.

One problem was also that the event was fired before the client started to listen to it. The best way is to debug with the pusher console and to fire custom events.

cre8
  • 13,012
  • 8
  • 37
  • 61
2

I have been using Laravel 5.4 events quite efficiently since 6 months with a project. I had same trouble when I was doing initial setup. Let me guide you with whatever I have done to get it working.

I can see you have controller to initiate an Event, SendMessageEvent to send message content to pusherjs. You need to check following stuff to get it going.

Check 1:

But you have not mentioned if you have an Eventhandler defined. Event handler works as a bridge between the SendMessageEvent and its actual broadcaster. So define an Eventhandler create one folders like app / Handlers / Events /. (here Handlers and Events are folders. where Events is inside Handlers)

create one file inside this Events folder with a name e.g.

HandleMyMessage.php And put this code in it:

<?php
namespace App\Handlers\Events;
use App\Events\SendMessageEvent;    // This should be your event class..
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HandleMyMessage{
   protected $name;

   public function __construct() {
      //
   }
   public function handle(Message $event) {
        // No need to write anything here
   }
}

Check 2: There should be one provider at app / Providers / EventServiceProvider.php location, and put following code in EventServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\SendMessageEvent' => [
            'App\Handlers\Events\EventServiceProvider',
        ],
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */
    public function boot()
    {
        parent::boot();
    }
}

Check 3: And you should change syntax of sending event message in your controller like this:

$broadcast_data['first_name'] = 'John';
$broadcast_data['last_name'] = 'Doe';
$return_socket['data'] = $broadcast_data;

Event::fire(new Message($return_socket));       //  Sending message to pusherjs via Laravel Event.

More on this, you also need Redis installed on your system and run it.

Hope this helps. If you need more information for any of above, just comment. I am posting a reference link for the same.

Happy coding!!! :-)

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

I got it working. Below is the correct code. I hope this will be useful to others for sending real time messaging.

Js Work

<script>
    window.Echo.channel('private-SendMessageChannel.{!! \Auth::user()->UserID !!}')
        .listen('SendMessageEvent', (e) => {
            console.log(e);
        });
</script>

Controller Code

broadcast(new SendMessageEvent("Hi", 1))->toOthers();
//Here 1 is recepient ID

Event Code

class SendMessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $Message;
    private $UserID;

    public function __construct($message, $RecepientID)
    {
        $this->Message = $message;
        $this->UserID = $RecepientID;
    }

    public function broadcastOn()
    {
        return new PrivateChannel('SendMessageChannel.' . $UserID);
    }

}
Pankaj
  • 9,749
  • 32
  • 139
  • 283
0

In my case, I had used:

    public function broadcastAs()
    {
        return 'message';
    }

If you've set the broadcast name, you must use that name in the listener differently.

For those who have done the same mistake as I did:

If you customize the broadcast name using the broadcastAs method, you should make sure to register your listener with a leading . character. This will instruct Echo to not prepend the application's namespace to the event:

https://laravel.com/docs/9.x/broadcasting#broadcast-name

X 47 48 - IR
  • 1,250
  • 1
  • 15
  • 28