Three Ways to Stream LLM Tokens into Vue: fetch, useChat, and Laravel Echo
Gentil Maliyamungu B. · 2026-11-15
The same Laravel AI SDK agent streamed into Vue three ways: fetch with SSE, useChat, and a queued run over Reverb and Echo, with the trade-offs.
When a Vue app talks to a Laravel backend that calls a language model, "stream the answer" can mean three quite different things:
- The browser POSTs and reads the response body as it arrives (Server-Sent Events over
fetch).
- The browser uses a chat library that speaks a richer protocol over the same kind of HTTP stream (
useChat from @ai-sdk/vue).
- The browser POSTs, gets a 202, and listens on a WebSocket while a queue worker runs the model and broadcasts events (Laravel Reverb and Echo).
The Laravel AI SDK supports all three out of the box. I built the same small support agent (it can look up an order with a LookupOrder tool) behind three routes, wrote a Vue client for each, and ran all three against a local Laravel app. This post compares them with working code and the details that only show up when you run them.
Tested with laravel/ai 1.4.0, Laravel 13.42, Reverb 1.13, ai 7.0.173, @ai-sdk/vue 4.0.173, @laravel/echo-vue 2.6.3 and Vue 3.5.
The three routes
use App\Ai\Agents\SupportAgent;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Http\Request;
// A: the SDK's default SSE event format.
Route::post('/chat/raw', function (Request $request) {
$validated = $request->validate(['message' => ['required', 'string', 'max:2000']]);
return (new SupportAgent)->stream($validated['message']);
})->middleware('throttle:ai');
// B: the Vercel UI message stream that useChat expects.
Route::post('/chat', [ChatController::class, 'store'])->middleware('throttle:ai');
// C: run on a queue, broadcast over Reverb.
Route::post('/chat/queued', function (Request $request) {
$validated = $request->validate(['message' => ['required', 'string', 'max:2000']]);
(new SupportAgent)->broadcastOnQueue(
$validated['message'],
new PrivateChannel('App.Models.User.'.$request->user()->id),
);
return response()->noContent(202);
})->middleware(['auth', 'throttle:ai']);
Route B's controller is Vercel::chat($request) plus ->stream($chat)->usingProtocol($chat->protocol()), covered in detail in the first article of this series. Here I focus on the client side of each.
A: fetch and the default SSE format
Returning ->stream() from a route sends text/event-stream with one JSON object per data: line. For "Where is order 1002?" the event types, in order, were:
stream_start, tool_call, tool_result, stream_start, text_start,
text_delta (x9), text_end, stream_end, [DONE]
There is one stream_start per model step (the tool call is step one, the answer is step two) but only one stream_end, at the very end. A text_delta looks like this:
{"id":"01a1debe-...","invocation_id":"01a1debe-...","type":"text_delta","message_id":"01a1debe-...","delta":"Order ","timestamp":1794738385}
The browser's EventSource only does GET requests, and you do not want the user's message in a query string. So read the stream with fetch:
// resources/js/composables/useFetchStream.js
import { ref } from 'vue';
export function useFetchStream(url) {
const text = ref('');
const tools = ref([]);
const status = ref('ready'); // ready | streaming | error
const error = ref(null);
let controller = null;
function handle(event) {
// Provider errors put the provider's error code in `type`, so detect them by shape.
if ('recoverable' in event && 'message' in event) {
throw new Error(event.message);
}
switch (event.type) {
case 'text_delta':
text.value += event.delta;
break;
case 'tool_call':
tools.value.push({ id: event.tool_id, name: event.tool_name, input: event.arguments, result: null });
break;
case 'tool_result': {
const call = tools.value.find((t) => t.id === event.tool_id);
if (call) call.result = event.successful ? event.result : `Failed: ${event.error}`;
break;
}
}
}
async function send(message) {
controller = new AbortController();
text.value = '';
tools.value = [];
error.value = null;
status.value = 'streaming';
let finished = false;
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'text/event-stream',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
},
body: JSON.stringify({ message }),
signal: controller.signal,
});
if (!response.ok) throw new Error(`Request failed with ${response.status}`);
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let buffer = '';
for (;;) {
const { value, done } = await reader.read();
if (done) break;
buffer += value;
// SSE frames are separated by a blank line.
let boundary;
while ((boundary = buffer.indexOf('\n\n')) !== -1) {
const frame = buffer.slice(0, boundary);
buffer = buffer.slice(boundary + 2);
for (const line of frame.split('\n')) {
if (!line.startsWith('data: ')) continue;
const data = line.slice(6);
if (data === '[DONE]') { finished = true; continue; }
handle(JSON.parse(data));
}
}
}
if (!finished) throw new Error('The stream ended before the server finished.');
status.value = 'ready';
} catch (e) {
if (e.name === 'AbortError') {
status.value = 'ready';
return;
}
error.value = e;
status.value = 'error';
}
}
const stop = () => controller?.abort();
return { text, tools, status, error, send, stop };
}
Two things in there came from running it, not from reading docs:
- Errors are not
type: "error". When I made the provider fail mid-stream, the event was {"type":"server_error","message":"The mock provider failed on purpose.","recoverable":false,...}. The type is the provider's own error code. The broadcasting section of the docs says the same thing about broadcast errors ("the provider's own error code is available in the payload's type key"). On the raw stream, I detect errors by the presence of message and recoverable.
- A failed stream has no
[DONE]. The connection just closes. Treat "the body ended without [DONE]" as a failure, or a half-written answer will look complete.
Frames can also be split across network chunks, which is why the code buffers until it sees a blank line instead of parsing each chunk directly.
Good for: a small widget, a one-shot "summarize this" button, or a client where you do not want a dependency. You own the parsing, the history and the tool rendering.
B: useChat
useChat does all of the above for you, against the Vercel UI message protocol:
import { useChat } from '@ai-sdk/vue';
import { DefaultChatTransport } from 'ai';
const { messages, status, error, sendMessage, stop } = useChat({
transport: new DefaultChatTransport({
api: '/chat',
headers: { 'X-CSRF-TOKEN': csrfToken },
}),
});
You get messages with typed parts (text, tool-LookupOrder with states such as input-available and output-available, reasoning, and so on), a status ref (submitted, streaming, ready, error), stop(), and conversation history that is sent with each request. Errors arrive as a protocol part ({"type":"error","errorText":"..."}) and land in error.
The real advantage shows up with tools. Human approval for risky tools is built into the protocol: the server sends a tool-approval-request part, and the client answers with addToolApprovalResponse(). With the raw SSE format you would design all of that yourself.
The trade-off: the protocol is defined by the Vercel AI SDK, which moves quickly. Between major versions the client API has changed (DefaultChatTransport, sendMessage, parts instead of content), so pin your versions and read the changelog when you upgrade.
Good for: most chat UIs where you control both the Laravel route and the Vue app.
C: queued runs broadcast with Reverb and Echo
The first two options hold an HTTP request open for the whole run, and so does the PHP worker serving it. If a run can take a long time, or you want it to continue when the user closes the tab, put the agent on a queue. broadcastOnQueue() dispatches a job, and the job broadcasts each stream event on the channel you give it.
On the Laravel side you need broadcasting and a queue worker:
php artisan install:broadcasting --reverb
php artisan reverb:start
php artisan queue:work
The default routes/channels.php already authorizes App.Models.User.{id} for the matching user, so I broadcast on that private channel.
Events are broadcast under their type, with custom names, so Echo listeners need a leading dot. With @laravel/echo-vue, useEcho handles subscribing on mount and leaving on unmount:
// resources/js/composables/useQueuedChat.js
import { ref } from 'vue';
import { useEcho } from '@laravel/echo-vue';
export function useQueuedChat(userId) {
const text = ref('');
const status = ref('ready'); // ready | waiting | streaming | error
const error = ref(null);
const channel = `App.Models.User.${userId}`;
useEcho(channel, '.text_delta', (event) => {
status.value = 'streaming';
text.value += event.delta;
});
useEcho(channel, '.stream_end', () => {
status.value = 'ready';
});
useEcho(channel, '.error', (event) => {
error.value = new Error(event.message);
status.value = 'error';
});
useEcho(channel, '.stream_failed', (event) => {
// A provider error is followed by stream_failed; keep the more specific message.
error.value ??= new Error(event.message ?? 'The queued run failed.');
status.value = 'error';
});
async function send(message) {
text.value = '';
error.value = null;
status.value = 'waiting';
const response = await fetch('/chat/queued', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
},
body: JSON.stringify({ message }),
});
if (response.status !== 202) {
error.value = new Error(`Request failed with ${response.status}`);
status.value = 'error';
}
}
return { text, status, error, send };
}
And configure Echo once, in your app's entry file:
import { configureEcho } from '@laravel/echo-vue';
configureEcho({ broadcaster: 'reverb' });
With only the broadcaster name, @laravel/echo-vue 2.6 fills in the key, host, port and scheme from the VITE_REVERB_* variables that install:broadcasting writes to your .env.
The payloads are the same JSON objects as in option A, so event.delta works the same way. Here is what I saw when I ran it:
- With a tool call, the broadcast sequence was the same as on the HTTP stream: two
stream_start, a tool_call, a tool_result, the text events, and one stream_end.
- When the provider failed, Echo received both
.error (with the provider's message) and .stream_failed (with "The stream failed."). The ??= keeps the useful message.
- The channel is per user, not per run. If the same user starts two runs at once (two tabs), their events mix. Every payload includes an
invocation_id, so you can lock onto the first one you see after sending and ignore the rest. Or allow one run at a time, as this composable assumes.
There is no built-in "stop" here. Aborting means cancelling a queued job, which you would have to build.
Good for: long runs, background work the user can leave, or agents you already trigger from jobs. The cost is two more processes (Reverb and a worker) and more to monitor.
Side by side
| A: fetch + SSE | B: useChat | C: queue + Echo |
| Request | POST, response streamed | POST, response streamed | POST returns 202, events over WebSocket |
| Laravel code | ->stream() | Vercel::chat() + usingProtocol() | ->broadcastOnQueue() |
| Extra infrastructure | none | none | Reverb (or Pusher/Ably) + queue worker |
| Tool calls in the UI | parse tool_call / tool_result yourself | tool-* parts with states | parse broadcast events yourself |
| Human approval | build it yourself | built into the protocol | events exist (tool_approval_request), wiring is yours |
| Stop / abort | AbortController | stop() | no built-in way |
| Survives tab close | no | no | yes, the job keeps running |
| Error signal | event with message + recoverable, then the body ends without [DONE] | error part, error ref | .error then .stream_failed |
| Client dependencies | none | ai, @ai-sdk/vue | laravel-echo, pusher-js, @laravel/echo-vue |
Which one I would pick
- Start with
useChat if you control both ends. You get tool parts, approvals and history handling for a few lines of code.
- Drop to fetch + SSE for a small widget that only shows text, or when you cannot add the AI SDK packages.
- Use the queue and Echo when the run must outlive the request, or the agent is already triggered from a job, a webhook, or a scheduled task.
Whatever you choose, check buffering in production. All three depend on something between PHP and the browser not holding data back: a proxy or CDN for A and B, the WebSocket path for C.
How the examples were tested
All three clients ran against a local Laravel 13 app with php artisan serve, reverb:start, queue:work, and a small scripted OpenAI-compatible server instead of a paid provider. Vitest with happy-dom ran the composables and components:
- A: the order-lookup answer and tool call parsed correctly, and a forced provider error produced
status: 'error' with the provider's message.
- B:
SupportChat.vue streamed the answer (the Stop button was visible mid-stream), and an error showed the error message.
- C:
useQueuedChat inside a mounted component received the queued answer over Reverb, then received .error and .stream_failed for a forced failure.
The companion demo includes the code for each approach, with Vitest coverage for the three Vue clients.