Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel echo is no listening

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.

like image 793
Alfredo Rodríguez Avatar asked Dec 04 '22 22:12

Alfredo Rodríguez


2 Answers

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

like image 42
webdevtr Avatar answered Dec 06 '22 12:12

webdevtr


If you using the Echo.channel(my-channel) for example you need to do something like this, attention to the ('.my-event') for some reason you need to a (.) before the event name.

Echo.channel(`my-channel`)
.listen('.my-event', (e) => {
    consolec.log(e);
});
like image 93
mario j goes tarosso Avatar answered Dec 06 '22 12:12

mario j goes tarosso