The title of the question can be a bit confusing, I tried my best to explain it.
I have laravel-echo-server , redis (for the queue) and Laravel echo . All I want is to send a message through the input and update the message channel for everyone on the page; essentially a chat.
Everything works, except that it is slow as hell, but I am new to Laravel echo and Vue .
if I sent one message, thatβs fine, but then when I send another message, I get two responses. Send the third message three answers and so on ...
What exactly am I doing wrong? I checked socket.io / laravel-echo-server only once connected to the channel, and my broadcast sent only once (checking that the worker from the queue processes only the request), since it repeats exponentially?
This is my app.js :
const chatWindow = new Vue({ el: '#chatWindow', data: { messages: [] }, methods: { sendMessage: function () { this.$http.post('/chat', {message: this.message}).then((response) => { window.Echo.channel('test-chat-channel') .listen('MessageSent', (event) => { console.log(event.message); // this.messages.push(event.message); }); }, (response) => { // error callback }); this.message = ''; } }, props: ['message'] });
This is my type of chat :
<div id="chatWindow" class="col-sm-7"> <h1 class="text-center no-top">Chat</h1> <div class="chat-window"> <ul> <li v-for="message in messages">@{{ message }}</li> </ul> <div class="form-group"> <input title="Send Message" v-model="message" @keyup.enter="sendMessage" class="form-control" placeholder="Send message..."> </div> </div> </div>
Just in case, they are relevant here, my route :
Route::post('/chat', function (Request $request) { event(new MessageSent($request->message)); });
and my event :
<?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Queue\SerializesModels; use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PresenceChannel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Contracts\Broadcasting\ShouldBroadcast; class MessageSent implements ShouldBroadcast { use InteractsWithSockets, SerializesModels; public $message; public function __construct($message) { $this->message = $message; } public function broadcastOn() { return new Channel('test-chat-channel'); } }