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

maryskolos's avatar

Livewire//Laravel: Concurrent Users on a Livewire component with API CRUD

We want to have a Livewire component that has concurrent users, but needs to update very quickly such that concurrent users do not create strange or repeat changes on a key model. We have an API, that is in a different repo, that has the Laravel CRUD controller for that particular key model for this Livewire component.

What is the best strategy to make sure that concurrent users get fast, updated data with minimal strain to the system?

0 likes
4 replies
LaryAI's avatar
Level 58

To handle concurrent users effectively in a Livewire component with an API-based CRUD system, you can follow these strategies:

  1. Optimistic Locking: Implement optimistic locking to prevent overwriting changes made by other users. This involves adding a version or timestamp column to your model. When updating a record, check if the version matches the current version in the database. If not, reject the update and notify the user.

  2. Polling for Updates: Use Livewire's polling feature to periodically fetch the latest data from the server. This ensures that users have the most up-to-date information without needing to refresh the page manually.

    class YourLivewireComponent extends \Livewire\Component
    {
        public $data;
    
        public function mount()
        {
            $this->fetchData();
        }
    
        public function fetchData()
        {
            $this->data = Http::get('your-api-endpoint')->json();
        }
    
        public function render()
        {
            return view('livewire.your-component');
        }
    }
    

    In your Blade view, you can use Livewire's wire:poll directive:

    <div wire:poll.5000ms="fetchData">
        <!-- Display your data here -->
    </div>
    
  3. WebSockets for Real-Time Updates: Consider using Laravel Echo and Pusher (or a similar WebSocket service) to push updates to the client in real-time. This is more efficient than polling and provides instant updates.

    • Set up Laravel Echo and Pusher in your Laravel application.
    • Broadcast events from your API when data changes.
    • Listen for these events in your Livewire component and update the state accordingly.
  4. Database Transactions: Use database transactions in your API to ensure that operations are atomic. This helps maintain data integrity when multiple users are making changes simultaneously.

  5. Caching: Implement caching strategies to reduce the load on your database. Use Laravel's caching mechanisms to store frequently accessed data and invalidate the cache when updates occur.

  6. Rate Limiting: Apply rate limiting to your API endpoints to prevent abuse and ensure fair usage among users.

By combining these strategies, you can create a robust system that handles concurrent users efficiently while minimizing strain on your server.

maryskolos's avatar

@vincent15000 Users that will be using the page and editing/interacting with the data at the same time.

1 like
vincent15000's avatar

@maryskolos If you really want to handle concurrent users, you need to lock the datas when a user edit thems and unlock when a user has updated them.

You can do that by adding a locked_at field and a locked_by field in the models.

Please or to participate in this conversation.