Search code examples
laravellaravel-echo

How to handle whisper in server side in laravel-echo?


I'm building realtime chat system.

this is front-end code.

onload

Echo.join(`chat.${id}`)
    .here(users => {})
    .listenForWhisper('sent', (e) => {});

on Sended

Echo.join(`chat.${id}`)
    .whisper('sent', {
        message : message,
        sender : sender,
    });

What I want to do is to handle the 'sent' whisper event in laravel like this

$message; // I want to get from whisper
$sender; // I want to get from whisper
Chat::create([
   "message" => $message,
   "sender" =>  $sender,
]);

But I don't how to implement this. So, I'm coding like this so far.

on Sended

Echo.join(`chat.${id}`)
    .whisper('sent', {
       "message" : message,
       "sender" : sender,
    });

axios.post('/message_sent', {
   message : message,
   sender : sender,
});

api.php

Route::post('/message_sent', function(Request $request) {
  Chat::create([
      "message" => $request['message'],
      "sender" => $request['sender'],
  ]);
});

in this case, the message(from client to server) is sended by HTTP, not TCP/IP.

Any one knows how to get whisper value in server side??


Solution

  • As the whisper event is only a client to client feature, it is not possible at the moment to retrieve the event with the Laravel Back-End.

    See: laravel.com/docs/broadcasting#client-events