Building modern web apps is hard.
Tools like Vue and React are extremely powerful, but the complexity they add to a full-stack developer's workflow is insane.
It doesn’t have to be this way...
Ok, I'm listening...
Say hello to Livewire.
Hi Livewire!
Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple, without leaving the comfort of Laravel.
Consider my interest piqued
It's not like anything you've seen before, the best way to understand it is just to look at the code. Strap on your snorkle, we're diving in.
...I'll get my floaties
Ok, let's see some code
Here's a real-time search component built with Livewire.
use Livewire\Component;
class SearchUsers extends Component
{
public $search = '';
public function render()
{
return view('livewire.search-users', [
'users' => User::where('username', $this->search)->get(),
]);
}
}
<div>
<input wire:model="search" type="text" placeholder="Search users..."/>
<ul>
@foreach($users as $user)
<li>{{ $user->username }}</li>
@endforeach
</ul>
</div>
You can include this component anywhere in your app like so.
<body>
...
@livewire('search-users')
...
</body>
When a user types into the search input, the list of users updates in real-time.
Bonkers, I know...
How the he*k does this work?
- Livewire renders the initial component output with the page (like a Blade include), this way it's SEO friendly.
- When an interaction occurs, Livewire makes an AJAX request to the server with the updated data.
- The server re-renders the component and responds with the new HTML.
- Livewire then intelligently mutates DOM according to the things that changed.
Some questions you might have...
Does this use websockets?
No, Livewire relies solely on AJAX requests to do all its server communication. This means it's as reliable and scalable as your current setup.
Is this a Vue-replacement?
In some ways yes, but mostly for cases where your Vue components are already sending `axios` or `fetch` requests. (Think searching, filtering, forms)
If it doesn't replace Vue, what do I do when I need JavaScript, like a drop-down, modal, or datepicker?
Livewire works beautifully with the AlpineJS framework (It was built for this need). For third-party library integration (something like Select2, Pickaday, or Dropzone.js), Livewire provides APIs to add support for these. Livewire also has a plugin to support using VueJs components inside of your Livewire components.