The realtime Laravel application made by socket.io, laravel-echo, redis
I am talking about is in localhost/br1
. I created a simple private event as follow
namespace App\Events;
use App\User;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class PrivateEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $user;
public function __construct(User $user)
{
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('user.' . $this->user->id);
}
}
and in channels.php
Broadcast::channel('user.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
and route
Route::get('test-broadcast', function(){
broadcast(new \App\Events\PrivateEvent(auth()->user()));
});
and script
<script>
window.Laravel = {!! json_encode([
'user' => auth()->check() ? auth()->user()->id : null,
]) !!};
</script>
which is in head and
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src=""></script>
<script type="text/javascript">
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
console.log("doc is ready");
window.Echo.private('user.'+window.Laravel.user)
.listen('PrivateEvent', (e) => {
console.log(e);
});
}
};
</script>
in welcome.blade.php
.
after running queue
and npm run dev
and starting laravel-echo-server
, I face two problems:
1- After starting laravel echo server or visiting the url containing welcome.blade.php
I see this
2- After visiting page /test-broadcast
the terminal prints message
Channel: private-user.2
Event: App\Events\PrivateEvent
what is this private-
? and how does this prefix will effect the way I must name and call the events?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire