Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Programming Real-Time Web Applications With TALL Stack
Yoram Kornatzky
Yoram Kornatzky

Posted on • Edited on • Originally published atyoramkornatzky.com

     

Programming Real-Time Web Applications With TALL Stack

Real-Time Web Applications

Real-time web applications reflect immediately to each user the concurrent actions of other users.

Widely used real-time applications include:

  • Auctions
  • Chat systems
  • Shared whiteboards
  • Collaborative document editors
  • Online education

The TALL Stack

The TALL stack is a prominent combined front-end and backend framework in the PHP world.

The stack:

  1. Laravel
  2. Alpine.js
  3. Livewire
  4. Tailwind CSS

The Triggering Action

Action in the Blade template of a Livewire component is triggered on a button click,

<input name="bid_price" wire:model="bid_price"/><button wire:click="bid()/>
Enter fullscreen modeExit fullscreen mode

Which invokes a function in the component,

namespace App\Http\Livewire;use Livewire\Component;class Bidder extends Component{    public float $bid_price;    public float $price;    public function bid()    {    }}
Enter fullscreen modeExit fullscreen mode

The Connecting Pipes

By broadcasting events in Laravel, the actions of users are propagated to other users.

So say we have an event that is broadcast when a user sends a bid,

namespace App\Events;use Illuminate\Broadcasting\Channel;use Illuminate\Broadcasting\InteractsWithSockets;use Illuminate\Broadcasting\PresenceChannel;use Illuminate\Broadcasting\PrivateChannel;use Illuminate\Contracts\Broadcasting\ShouldBroadcast;use Illuminate\Foundation\Events\Dispatchable;use Illuminate\Queue\SerializesModels;use App\Models\Auction;use App\Models\User;class LiveBid implements ShouldBroadcast{    use Dispatchable, InteractsWithSockets, SerializesModels;    public function __construct(    private Auction $auction,    private $item,    private $user,    private float $bid_price,    )    {    //    }    /**     * Get the data to broadcast.     *     * @return array    */    public function broadcastWith()    {      return [        'item' => $this->item,        'type' => $this->type,        'bid' => $this->bid_price,        'user' => $this->user,      ];    }}
Enter fullscreen modeExit fullscreen mode

Using:

use App\Events\LiveBid; broadcast(new LiveBid(    $this->auction,     $this->item,     $this->user,     $bid_price))->toOthers();
Enter fullscreen modeExit fullscreen mode

How the Events Flow

UsingLaravel Websockets, broadcast events are sent to Livewire components.

Components list the events they listen to,

namespace App\Http\Livewire;use Livewire\Component;class Bidder extends Component{    public float $price;    protected function getListeners()    {             return [        "echo:auction.{$this->auction->id},LiveBid"                  => 'live_price_notification',      ];    }  }
Enter fullscreen modeExit fullscreen mode

Where we have a channel per auction,

"echo:auction.{$this->auction->id},LiveBid"
Enter fullscreen modeExit fullscreen mode

with many auctions running in parallel.

On receiving the event, the listener calls the assigned function with the data that was broadcast as an array,

public function live_price_notification($data){      // update price      $this->price = $data['price'];}
Enter fullscreen modeExit fullscreen mode

Dynamic Update to the Front-End

So for the Bidder component, its Blade template,bidder.blade.php is automatically dynamically updated by Livewire once the price property is updated,

Price: {{ $price }} EUR
Enter fullscreen modeExit fullscreen mode

Summing up, the flow is,

Livewire -> Livewire
Enter fullscreen modeExit fullscreen mode

Where no JavaScript was needed.

This is the more straightforward case.

When We Need JavaScript

In a whiteboard application, we must use JavaScript to listen to user actions, such as moving the mouse, as they are purely browser events. Consequently, to reflect the actions of other users, we must render things at the browser level, such as pointer positions on the screen, for which we must use JavaScript.

The TALL stack uses the lightweight Alpine.js framework to do the browser lifting.

To integrate the actions from the browser into the Livewire component, we use Alpine.jsLivewire.emit,

Livewire.emit('MouseMoved', x, y)
Enter fullscreen modeExit fullscreen mode

To dynamically update the front-end, say the position on the canvas, we use in the Livewire component,

$this->dispatchBrowserEvent('SetPointerPosition', [       'x' => $x, 'y' => $y]);
Enter fullscreen modeExit fullscreen mode

Summing up, the flow is,

Alpine.js -> Livewire -> Livewire -> Alpine.js
Enter fullscreen modeExit fullscreen mode

A Recap

For PHP lovers, the TALL stack supplies a superb way to program real-time web applications with a pure PHP playbook.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Entrepreneur. Co-Founder Auctibles. Developing: auctions, marketplaces, PHP, Laravel. 25 years of dev experience. Ph.D. Computer Science.
  • Location
    Warsaw, Poland
  • Education
    Ph.D. Computer Science
  • Work
    Entrepreneur and Freelance Consultant
  • Joined

More fromYoram Kornatzky

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp