8

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.

8 Answers8

30

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);
});
4

I hope someone find this helpful, also faced a lot of hours to fix echo to listen to my events and here's how I've solved it. I'm using Laravel 7 and pusher server 4.0

see: https://laravel.com/docs/7.x/broadcasting#driver-prerequisites

MyEvent.php

public function broadcastOn()
{
    return new Channel('my-channel');
}

public function broadcastAs()
{
    return 'my-event';
}

resources/js/bootstrap.js

 import Echo from 'laravel-echo';

 window.Pusher = require('pusher-js');

 window.Echo = new Echo({
     broadcaster: 'pusher',
     key: process.env.MIX_PUSHER_APP_KEY,
     cluster: process.env.MIX_PUSHER_APP_CLUSTER,
     encrypted:true,
     forceTLS: true
 });

resources/views/your_views.blade.php

Echo.channel(`my-channel`)
.listen('.my-event', (e) => {
    console.log('Test');
});

Note: If you declare broadcastAs() make sure in your .listen() formatted like the above code.

Note: Make sure to run "npm run dev" everytime you have changes in app.js or bootstrap.js file.

xrak
  • 111
  • 6
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

webdevtr
  • 480
  • 2
  • 6
2

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

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

Parth Vora
  • 4,073
  • 7
  • 36
  • 59
0

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

omarjebari
  • 4,861
  • 3
  • 34
  • 32
0

Check the version of "socket.io-client", you need to use 2.x instead of 3.x

pidan
  • 1
  • 1
0

I did everything above but only downgrading socket.io-client to 2.3.0 works :( https://github.com/tlaverdure/laravel-echo-server/issues/550

AmirRezaM75
  • 1,030
  • 14
  • 17
  • At least one other answer as already named using 2.x instead of 3.x. Please make more obivous which additional insight your post contributes. Note that what you only link, without at least summarising it directly in your post is not considered part of your answer. – Yunnosch Oct 03 '21 at 14:39