npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@laravel/stream-vue

v0.3.10

Published

Laravel streaming hooks for Vue

Readme

Laravel Stream for Vue

Easily consume streams in your Vue application.

Installation

npm install @laravel/stream-vue

Streaming Responses

[!IMPORTANT] The useStream hook is currently in Beta, the API is subject to change prior to the v1.0.0 release. All notable changes will be documented in the changelog.

The useStream hook allows you to seamlessly consume streamed responses in your Vue application.

Provide your stream URL and the hook will automatically update data with the concatenated response as data is returned from your server:

<script setup lang="ts">
import { useStream } from "@laravel/stream-vue";

const { data, isFetching, isStreaming, send } = useStream("chat");

const sendMessage = () => {
    send({
        message: `Current timestamp: ${Date.now()}`,
    });
};
</script>

<template>
    <div>
        <div>{{ data }}</div>
        <div v-if="isFetching">Connecting...</div>
        <div v-if="isStreaming">Generating...</div>
        <button @click="sendMessage">Send Message</button>
    </div>
</template>

When sending data back to the stream, the active connection to the stream is canceled before sending the new data. All requests are sent as JSON POST requests.

The second argument given to useStream is an options object that you may use to customize the stream consumption behavior:

type StreamOptions = {
    id?: string;
    initialInput?: Record<string, any>;
    headers?: Record<string, string>;
    csrfToken?: string;
    json?: boolean;
    credentials?: RequestCredentials;
    onResponse?: (response: Response) => void;
    onData?: (data: string) => void;
    onCancel?: () => void;
    onFinish?: () => void;
    onError?: (error: Error) => void;
    onBeforeSend?: (request: RequestInit) => boolean | RequestInit | void;
};

onResponse is triggered after a successful initial response from the stream and the raw Response is passed to the callback.

onData is called as each chunk is received, the current chunk is passed to the callback.

onFinish is called when a stream has finished and when an error is thrown during the fetch/read cycle.

onBeforeSend is called right before sending the request to the server and receives the RequestInit object as an argument. Returning false from this callback cancels the request, returning a RequestInit object will override the existing RequestInit object.

By default, a request is not made the to stream on initialization. You may pass an initial payload to the stream by using the initialInput option:

<script setup lang="ts">
import { useStream } from "@laravel/stream-vue";

const { data } = useStream("chat", {
    initialInput: {
        message: "Introduce yourself.",
    },
});
</script>

<template>
    <div>{{ data }}</div>
</template>

To cancel a stream manually, you may use the cancel method returned from the hook:

<script setup lang="ts">
import { useStream } from "@laravel/stream-vue";

const { data, cancel } = useStream("chat");
</script>

<template>
    <div>
        <div>{{ data }}</div>
        <button @click="cancel">Cancel</button>
    </div>
</template>

Each time the useStream hook is used, a random id is generated to identify the stream. This is sent back to the server with each request in the X-STREAM-ID header.

When consuming the same stream from multiple components, you can read and write to the stream by providing your own id:

<!-- App.vue -->
<script setup lang="ts">
import { useStream } from "@laravel/stream-vue";
import StreamStatus from "./StreamStatus.vue";

const { data, id } = useStream("chat");
</script>

<template>
    <div>
        <div>{{ data }}</div>
        <StreamStatus :id="id" />
    </div>
</template>
<!-- StreamStatus.vue -->
<script setup lang="ts">
import { useStream } from "@laravel/stream-vue";

const props = defineProps<{
    id: string;
}>();

const { isFetching, isStreaming } = useStream("chat", { id: props.id });
</script>

<template>
    <div>
        <div v-if="isFetching">Connecting...</div>
        <div v-if="isStreaming">Generating...</div>
    </div>
</template>

The useJsonStream hook is identical to the useStream hook except that it will attempt to parse the data as JSON once it has finished streaming:

<script setup lang="ts">
import { useJsonStream } from "@laravel/stream-vue";

type User = {
    id: number;
    name: string;
    email: string;
};

const { data, send } = useJsonStream<{ users: User[] }>("users");

const loadUsers = () => {
    send({
        query: "taylor",
    });
};
</script>

<template>
    <div>
        <ul>
            <li v-for="user in data?.users" :key="user.id">
                {{ user.id }}: {{ user.name }}
            </li>
        </ul>
        <button @click="loadUsers">Load Users</button>
    </div>
</template>

Event Streams (SSE)

The useEventStream hook allows you to seamlessly consume Server-Sent Events (SSE) in your Vue application.

Provide your stream URL and the hook will automatically update the message with the concatenated response as messages are returned from your server:

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";

const { message } = useEventStream("/stream");
</script>

<template>
    <div>{{ message }}</div>
</template>

You also have access to the array of message parts:

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";

const { messageParts } = useEventStream("/stream");
</script>

<template>
    <ul>
        <li v-for="message in messageParts">
            {{ message }}
        </li>
    </ul>
</template>

If you'd like to listen to multiple events:

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";

useEventStream("/stream", {
    eventName: ["update", "create"],
    onMessage: (event) => {
        if (event.type === "update") {
            // Handle update
        } else {
            // Handle create
        }
    },
});
</script>

The second parameter is an options object where all properties are optional (defaults are shown below):

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";

const { message } = useEventStream("/stream", {
    event: "update",
    onMessage: (message) => {
        //
    },
    onError: (error) => {
        //
    },
    onComplete: () => {
        //
    },
    endSignal: "</stream>",
    glue: " ",
    replace: false,
});
</script>

You can close the connection manually by using the returned close function:

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";
import { onMounted } from "vue";

const { message, close } = useEventStream("/stream");

onMounted(() => {
    setTimeout(() => {
        close();
    }, 3000);
});
</script>

<template>
    <div>{{ message }}</div>
</template>

The clearMessage function may be used to clear the message content that has been received so far:

<script setup lang="ts">
import { useEventStream } from "@laravel/stream-vue";
import { onMounted } from "vue";

const { message, clearMessage } = useEventStream("/stream");

onMounted(() => {
    setTimeout(() => {
        clearMessage();
    }, 3000);
});
</script>

<template>
    <div>{{ message }}</div>
</template>

License

Laravel Stream is open-sourced software licensed under the MIT license.