Laravel Tutorials

Ship AI with Laravel: Real-Time Streaming Chat UI with Livewire

Published

▶️ Watch the video tutorial (10 minutes)

So far we've been testing the agent through JSON routes. It returns the full reply once the AI is done. Works for development, terrible for customers. Nobody wants to stare at a spinner for five seconds wondering if anything is happening.

In this episode we build a real-time chat widget where the response streams in word by word, like ChatGPT. The user types a question, hits send, and the agent's reply starts appearing immediately as it generates.

The SDK makes this a one-line change. Instead of calling prompt() we call stream(), and the SDK forwards token deltas to the browser via server-sent events. I add a stream method to the ChatController that handles SSE with proper headers, then reuse a private resolveAgent method to start or continue conversations for the authenticated user.

The frontend is a Livewire component for state, a Blade template for the UI, and Alpine.js for the streaming logic. The Alpine code opens a fetch readable stream, parses each chunk as it arrives, and appends the text to the message bubble live. No external dependencies. Just Livewire, Alpine, and the SDK doing the heavy lifting.

I also cover a small gotcha around capturing the conversation ID on the first message. The fix is a completion callback that fires once the stream finishes, so we can store the ID for follow-up messages.

By the end the chat feels like a real product. Same agent, same tools, same knowledge base, just streamed.

Next episode we give the agent access to the live web with the WebSearch tool, so it can answer questions about things that change in real time like shipping carrier delays.

⭐ GitHub: https://github.com/harris21/ship-ai-with-laravel

▶️ Watch Episode 7 now →

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

masteringlaravel logo
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review

The latest

View all →
Validate and Convert HEIC Images in Laravel image

Validate and Convert HEIC Images in Laravel

Read article
Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues image

Saga Lara Flow: Durable Workflows and Compensating Transactions on Laravel Queues

Read article
Reject Unexpected Array Keys with Laravel Validation image

Reject Unexpected Array Keys with Laravel Validation

Read article
Major performance improvements & security patches for Filament v4.12 and v5.7! image

Major performance improvements & security patches for Filament v4.12 and v5.7!

Read article
Laravel Boost Project Rules: Teach Agents Your Conventions image

Laravel Boost Project Rules: Teach Agents Your Conventions

Read article
Extract an Image's Dominant Color in Laravel image

Extract an Image's Dominant Color in Laravel

Read article