Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jorge_dev96's avatar

I don't receive a broadcast message in the front end laravel-echo-server

I'm currently working chat webpage with laravel-echo-server package in Laravel, I'm trying to show a message to send a message to one user and receive that message in realtime, but when I send a message it loads in my chat but the other user doesn't receive anything until I refresh the site. I think that laravel-echo-server isn't broadcasting correctly, but I don't know how to how to solve it. I checked if the event was correctly executed and it does but I dont' receive nothing. Can you help me? I'm using redis has a broadcaster and it seems to work correctly I added it to .env as a BROADCAST_DRIVER=redis and QUEUE_CONNECTION=redis

ChatApp.vue

     ......
mounted() {
    console.log(this.user);
    Echo.private(`messages.${this.user.id}`)
     .listen('NewMessage', (e) => {
          console.log(e);
          if (this.selectedContact && e.message.from == this.selectedContact.id){
                    this.saveNewMessage(e.message);
                    alert('fired');
                }
      });

channels.php authenticate messages between users


Broadcast::channel('messages.{id}', function ($user, $id) {
    //dd($user->id, $id);
    var_dump($user);
    return (int) $user->id === (int) $id;
});

NewMessage.php This is my event

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];
    }
}

ContactsController.php I use send fuction when I user send a message to a other user and after I broadcast


Im getting this in my terminal when a user send a message to other user but the other user doesn't receive noting until I refresh


C:\laragon\www\newchat
λ laravel-echo-server start

L A R A V E L  E C H O  S E R V E R

version 1.6.1

⚠ Starting server in DEV mode...

✔  Running at localhost on port 6001
✔  Channels are ready.
✔  Listening for http events...
✔  Listening for redis events...

Server ready!

[15:28:39] - Preparing authentication request to: http://newchat.test
[15:28:39] - Sending auth request to: http://newchat.test/broadcasting/auth

[15:28:40] - hq3MFtdUmFgfBB7WAAAA authenticated for: private-messages.2
[15:28:40] - hq3MFtdUmFgfBB7WAAAA joined channel: private-messages.2
[15:28:59] - Preparing authentication request to: http://newchat.test
[15:28:59] - Sending auth request to: http://newchat.test/broadcasting/auth

[15:29:00] - hf8Ginp5is-fyZLGAAAC authenticated for: private-messages.1
[15:29:00] - hf8Ginp5is-fyZLGAAAC joined channel: private-messages.1
Channel: chatapp_database_private-messages.7
Event: App\Events\NewMessage
...
class ContactsController extends Controller
{
    public function get()
    {
        $contacts = User::where('id', '!=' ,auth()->id())->get();
        return response()->json($contacts);
    }

    public function getMessagesFor($id)
    {
        $messages = Message::where('from', $id)->orWhere('to', $id)->get();
        return response()->json($messages);
    }
    public function send(Request $request)
    {
        $message = Message::create([
            'from' => auth()->id(),
            'to' => $request->contact_id,
            'text' => $request->text
        ]);
        broadcast(new NewMessage($message));
        return response()->json($message);
    }
}

...

Also Im using this command executed together with laravel-echo-server

 php artisan queue:listen --tries=1
0 likes
2 replies
Tippin's avatar
Tippin
Best Answer
Level 13

@jorge_dev96 It could be the event name your Echo is listening for. By default laravel will broadcast the event's class name and prepend the namespace as well, so you may be receiving App\Events\NewMessage and did not setup Echo namespace. I prefer to name my events instead using their broadcastAs method. Then in your listener you use your defined name prepended with a "." as such:

NewMessage Event class add

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

ChatApp.vue

    Echo.private(`messages.${this.user.id}`)
        .listen('.NewMessage', (e) => {
          console.log(e);
          if (this.selectedContact && e.message.from == this.selectedContact.id){
                    this.saveNewMessage(e.message);
                    alert('fired');
                }
      });
2 likes

Please or to participate in this conversation.