Laravel echo is no listening

。_饼干妹妹 提交于 2020-12-06 09:00:50

问题


i'm from venezuela, i've a problem tryging to integrate laravel echo with a project, i try everything and i wanna receive your help.

maybe i'm not working with the right way.

i'm trying to use broadcasting events with socket io because i'm working in a startup and is very difficult pay a services like pusher.

i'm trying to do a simple real time chat..

That is my chatController..

public function index(Request $request){

  $sender=Auth::user()->id;
  $receiver=\App\User::find(2);
  $message=new Message;
  $message->message="Hola, esto es una prueba de eventos";
  $message->user_receiver_id=$receiver->id;
  $message->user_sender_id=Auth::user()->id;
  $message->id_chat=1;
  $message->save();

  event(new \App\Events\sendMessage($message,$receiver));

  return response()->json(["status"=>"ok"]);
}

This chat store in a database the messages according to determinate group (chat).

The event that will be fired is:

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

/**
 * Create a new event instance.
 *
 * @return void
 */

public $message;
public $user_receiver;

public function __construct(Message $message,User $user_receiver)
{
    $this->message=$message;
    $this->user_receiver=$user_receiver;

}

public function broadcastOn()
{
    \Log::info($this->message->id_chat);

    return new PrivateChannel('chat.1');


}}

My app.js is:

require('./bootstrap');
import Echo from "laravel-echo";

window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001'
});


window.Echo.channel('chat.1')
.listen('sendMessage', e => {
    console.log(e);
});

at the first instance i just wanna get the public variables from my sendMessages event, after this i will to build the view.

I run the php artisan listener and i run de laravel echo server.

when i send a message this happen with the laravel echo server and the listener:

Laravel echo server output

enter image description here

I think that the process work fine but i think that i only have a problem with the client..

because nothing happen in the client when i send the message.

Sorry for my english, i hope that you can help me.


回答1:


This problem is very hard to troubleshoot without complete source code and running app.

I have implemented all 3 types of channels(private, public & presence) in this chat app, maybe you can get some ideas from it:

https://github.com/xparthx/laravel-realtime-chat

Thanks




回答2:


You should only use use SerializesModels;

Also, broadcast(new \App\Events\sendMessage($message,$receiver));

You can find Laravel 5 Broadcasting example here,

https://github.com/durmus-aydogdu/real-time-application




回答3:


In my case I am using Redis I noticed that there is a prefix for my channel name projectName_database, Laravel> 5.7 automatically adds a prefix to your channel name, you can delete the prefix in config / database.php Hope it helps someone




回答4:


You are broadcasting in Laravel on a PrivateChannel so you need to use the following when connecting with Echo:

window.Echo.private('chat.1').listen('sendMessage', e => {console.log(e);});

If you're still having problems then have a look at this reply



来源:https://stackoverflow.com/questions/43927792/laravel-echo-is-no-listening

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!