Sarıkaya Dev Logo

Build Real‑Time Full‑Stack Apps with Laravel, Supabase Edge Functions, and Livewire

Mahmut Sarıkaya 4 min read 2 Views 0
Build Real‑Time Full‑Stack Apps with Laravel, Supabase Edge Functions, and Livewire

Why Real‑Time Matters

Imagine a chat application where a message appears on every screen the instant it is sent—no refresh, no delay. According to a 2023 Stack Overflow survey, 68% of developers consider real‑time capabilities a top priority for modern web products. The user experience gap between static pages and live updates can translate into a 25% increase in engagement, especially for collaborative tools, dashboards, and social feeds.

Setting Up Laravel and Supabase

The foundation starts with a fresh Laravel 10 installation on PHP 8.2. After confirming composer --version returns 2.5+, run composer create-project laravel/laravel realtime-app. Next, create a Supabase project (free tier provides 500 MB storage and 500 k realtime messages per month). In the Supabase dashboard, enable the Realtime add‑on and generate an API key. Store the URL and key in .env as SUPABASE_URL and SUPABASE_KEY. Laravel can now communicate through the official supabase/supabase-php client.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\MessageController;

Route::post('/api/message', [MessageController::class, 'store']);

Creating Edge Functions for Instant Updates

Supabase Edge Functions run on Deno, allowing you to write lightweight JavaScript that lives next to your database. A typical function receives a POST request, inserts a row, then pushes the new record through the Realtime channel. Deploy the function with supabase functions deploy new-message and expose it at /functions/v1/new-message. The latency is usually under 150 ms, which is fast enough for most interactive apps.

export default async (request, context) => {
  const { body } = await request.json();
  const { data, error } = await context.supabase
    .from('messages')
    .insert([{ content: body.content, user_id: body.userId }]);

  if (error) {
    return new Response(JSON.stringify({ error }), { status: 400 });
  }

  // broadcast via Realtime
  await context.supabase
    .channel('public:messages')
    .send({ type: 'broadcast', payload: data[0] });

  return new Response(JSON.stringify({ success: true, message: data[0] }));
};

Integrating Livewire for Reactive UI

Livewire bridges Laravel Blade and JavaScript without writing a single Vue or React component. By listening to a custom event that the Edge Function emits, a Livewire component can prepend the new message to its local array, causing the DOM to update instantly. The component also fetches the initial message list via Laravel's HTTP client, which in turn calls Supabase’s REST endpoint.

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use Illuminate\Support\Facades\Http;

class Chat extends Component
{
    public $messages = [];

    protected $listeners = ['newMessage' => 'addMessage'];

    public function mount()
    {
        $this->loadMessages();
    }

    public function loadMessages()
    {
        $this->messages = Http::get(config('services.supabase.url') . '/rest/v1/messages')
            ->json();
    }

    public function addMessage($payload)
    {
        $this->messages[] = $payload;
    }

    public function render()
    {
        return view('livewire.chat');
    }
}
<div>
    <ul>
        <?php foreach($messages as $msg): ?>
            <li><?= e($msg['content']) ?></li>
        <?php endforeach; ?>
    </ul>
</div>

Putting It All Together – A Mini Project

Start by scaffolding a MessageController that forwards the incoming payload to the Supabase Edge Function using Http::post. In the Blade layout, include @livewireScripts and @livewireStyles. When a user submits a form, Livewire calls store() on the controller, which triggers the Edge Function. As soon as Supabase broadcasts the new row, the Livewire listener fires, updating the UI without a full page reload.

Performance Tips and Common Pitfalls

1. Cache the Supabase JWT for at least 30 minutes; repeated token generation adds 20‑30 ms per request. 2. Limit the Realtime payload to essential columns (e.g., content and user_id) to keep the bandwidth under the free tier limit. 3. When scaling, consider moving the Edge Function to a dedicated Deno Deploy project to avoid cold starts after periods of inactivity.

Conclusion

Combining Laravel’s expressive backend, Supabase Edge Functions for near‑instant database triggers, and Livewire’s seamless front‑end reactivity creates a powerful stack for real‑time applications. The workflow eliminates the need for a separate Node.js server, reduces latency, and lets developers stay within the familiar Laravel ecosystem while still delivering the live experience users expect.

Sources

  • Laravel Official Documentation
  • Supabase Edge Functions Guide
  • Livewire Docs – Realtime UI Patterns

Author: Mahmut Sarıkaya — sarikayadev.com

Tags: #laravel #supabase #edge functions #livewire #real-time
Share:
M

Written by

Mahmut Sarıkaya

Software Developer

Comments

No comments yet. Be the first to share your thoughts!

Leave a Comment

7 + 7 =