Livewire realtime chat appliation
view.users.chat
@extends('layouts.app')
@section('content')
<div class="container">
@livewire('messages')
</div>
@endsection
view/livewire/messages.blade.php
<div>
<div class="row justify-content-center">
<div class="col-md-4">
<div class="card">
<div class="card-header">
Users
</div>
<div class="card-body chatbox p-0">
<ul class="list-group list-group-flush">
@foreach($users as $user)
@if($user->id !==auth()->id())
@php
$not_seen = \App\Models\Message::where('user_id', $user->id)->where('receiver', auth()->id())->where('is_seen', false)->get() ?? null
@endphp
<a wire:click="getUser({{ $user->id }})" class="text-dark link">
<li class="list-group-item">
<img class="img-fluid avatar" src="https://cdn.pixabay.com/photo/2017/06/13/12/53/profile-2398782_1280.png">
@if($user->is_online) <i class="fa fa-circle text-success online-icon"></i> @endif {{ $user->name }}
@if(filled($not_seen))
<div class="badge badge-success rounded">{{ $not_seen->count() }}</div>
@endif
</li>
</a>
@endif
@endforeach
</ul>
</div>
</div>
</div>
<div class="col-md-8">
<div class="card">
<div class="card-header">
@if(isset($sender)) {{ $sender->name }} @endif
</div>
<div class="card-body message-box" wire:poll="mount">
@if(filled($messages))
@foreach($messages as $message)
<div class="single-message @if($message->user_id !== auth()->id()) received @else sent @endif">
<p class="font-weight-bolder my-0">{{ $message->user->name }}</p>
{{ $message->message }}
<br><small class="text-muted w-100">Sent <em>{{ $message->created_at }}</em></small>
</div>
@endforeach
@else
No messages to show
@endif
</div>
@if(isset($sender))
<div class="card-footer">
<form wire:submit.prevent="SendMessage">
<div class="row">
<div class="col-md-8">
<input wire:model="message" class="form-control input shadow-none w-100 d-inline-block" placeholder="Type a message" required>
</div>
<div class="col-md-4">
<button class="btn btn-primary d-inline-block w-100"><i class="far fa-paper-plane"></i> Send</button>
</div>
</div>
</form>
</div>
@endif
</div>
</div>
</div>
</div>
App/Livewire/messages.php
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\User;
class Messages extends Component
{
public $users;
public $messages;
public $sender;
public $message;
public $not_seen;
public function render()
{
$this->users=User::all();
return view('livewire.messages', [
'users' => $this->users,
'messages' => $this->messages,
'sender' => $this->sender
]);
}
public function mount() {
if(isset($this->sender->id)){
$this->messages = \App\Models\Message::where('user_id', auth()->id())->where('receiver', $this->sender->id)->orWhere('user_id', $this->sender->id)->where('receiver', auth()->id())->orderBy('id', 'DESC')->get();
$not_seen = \App\Models\Message::where('user_id', $this->sender->id)->where('receiver', auth()->id());
$not_seen->update(['is_seen' => true]);
}
}
public function SendMessage() {
$new_message = new \App\Models\Message();
$new_message->message = $this->message;
$new_message->user_id = auth()->id();
$new_message->receiver = $this->sender->id;
$new_message->save();
}
public function getUser($user_id) {
$this->sender = User::find($user_id);
$this->messages =\App\Models\Message::where('user_id', auth()->id())->where('receiver', $user_id)->orWhere('user_id', $user_id)->where('receiver', auth()->id())->orderBy('id', 'DESC')->get();
}
}
App/Http/Middleware/OnlineMiddleware.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\User;
use Illuminate\Support\Facades\Session;
class OnlineMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
$users_to_offline = User::where('last_activity', '<', now());
$users_to_online = User::where('last_activity', '>=', now());
if (isset($users_to_offline)) {
$users_to_offline->update(['is_online' => false]);
}if (isset($users_to_online)) {
$users_to_online->update(['is_online' => true]);
}
if (auth()->check()) {
$cache_value = Cache::put('user-is-online', auth()->id(), \Carbon\Carbon::now()->addMinutes(1));
$user = User::find(Cache::get('user-is-online'));
$user->last_activity = now()->addMinutes(1);
$user->is_online = true;
$user->save();
} elseif(!auth()->check() and filled(Cache::get('user-is-online'))) {
$user = User::find(Cache::get('user-is-online'));
if (isset($user)) {
$user->is_online = false;
$user->save();
}
}
return $next($request);
}
}
App/htttp/kernel.php
'web' => [
\App\Http\Middleware\OnlineMiddleware::class,
]
Route
Route::group(['middleware' => 'auth'], function () {
Route::view('chat','users.chat');
});
No comments