Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

leyeaa's avatar

Livewire Poll not working

Please i am developing a chat application and i'm using livewire component for the conversation part with polling to refresh the component so as to keep fetching the updated messages but this is not working effectively. I have to refresh the whole page before seeing the latest message. But i however notice that if I delete one of the messages directly in the database, the component reload and the message i deleted will disappear from the conversation which imply that the polling is working but i don't know why new message does not appear when added until the whole page is reloaded.

Below is the code for the component and class

<div wire:poll.keep-alive.2s>
        <ul>
            @foreach ($this->conversations as $message)
                <li>
                    <div class="singel-reviews {{Auth::user()->matricno==$message->matricno ? 'sender':'responder'}}">
                        <div class="reviews-author">
                            <div class="author-thum">
                                <img src="{{asset('profile_pictures/'.$message->user->alumniProfile->profile_picture)}}" class="messagePic" alt="Reviews">
                            </div>
                            <div class="author-name">
                                <h6>{{$message->user->name}}</h6>
                                <span>{{$message->created_at->format('H:i:s d-m-Y')}}</span>
                            </div>
                        </div>
                        <div class="reviews-description pt-10">
                            <p>{{$message->body}}</p>
                        </div>
                        
                    </div> <!-- singel reviews -->
                </li>
            @endforeach
        </ul>
</div>


namespace App\Livewire;

use App\Models\Thread;
use Livewire\Component;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

class AlumniChat extends Component
{
    public $receiver;
    public $thread_id,$conversations;

    public function mount(){
        $this->thread_id=DB::table('participants')->whereIn('matricno',[Auth::user()->matricno,$this->receiver])->groupBy('thread_id')->havingRaw('COUNT(*) > 1')->value('thread_id');
        if(!empty($this->thread_id)){
            $this->conversations=Thread::find($this->thread_id)->messages;
        }else{
            $this->conversations="";
        }
    }

    public function render()
    {
        return view('livewire.alumni-chat');
    }
}

Please note I've tried passing the conversations to the component view through the render method or calling it directly in the view but both is the same result. I need a kind advise on what I am doing wrong, I don't want to use pusher

0 likes
6 replies
leyeaa's avatar

@whoisthisstud Thank you for your response $this->conversations above define in the mount method return all the messages from the database If i understand your question. I am really new to Livewire

whoisthisstud's avatar

@leyeaa You said, "but i don't know why new message does not appear when added until the whole page is reloaded."

Where are you creating new records? If manually in the database, then using mount() will only grab new records when initially loading the component. Put the code, that is within the mount() method, and put it into the render() method. Then when you manually add a record to the database, wire:poll should work as you expect it.

leyeaa's avatar

@whoisthisstud Oh Great! I moved the codes under mount() method to render() and it work as expected. Thanks

I am using two browsers to test the chat application on my system. I however observed that after leaving the two conversations open for a while, by the time I click on the tabs again, there is a popup like modal showing 500 SERVER ERROR for one of the users I'm using in the conversation while for the other user there is popup with error of SQLSTATE[HY000] [1045] Access denied for user 'forge'@'localhost' (using password: NO) Is it that their login session timed out or what? I will appreciate further help please. Thank you

Merklin's avatar

And what happens when the pool is fired during the time a user is writing a message? If the whole "messages" box is refreshed I presume this also includes the send message form, which means the writing of a new message will be interrupted. I also have a chat application in a project and I am using notifications and pusher for the chat.

As for the issue with the 500 server error, edit config/database.php and in the section for the mysql edit:

            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
leyeaa's avatar

@Merklin Thank you very much for your response. I made the send message form a livewire component on it own and also the messages/chat thread a component. I have a blade view file that I then call and link the two components to a single page. So it's only the component for the messages that get refreshed alone without interrupting the sending of a new message.

Please or to participate in this conversation.