@minch/use-event-source

A Vue composable for simplifying Server-Sent Events.
3,766 2
Install
npm install @minch/use-event-source
Latest Version:1.3.20
Vue:3
License:MIT
Last Updated:Aug 13, 2026
Links: GitHub  ·  npm
Maintainer: oneminch

useEventSource

useEventSource is a Vue composable for simplifying Server-Sent Events (SSE).

It is based on the standard EventSource interface, and makes working with SSEs simpler in Vue & Nuxt applications.

Installation

npm install @minch/use-event-source

Usage

An example directory structure of a simple Nuxt 3 app:

sse-app/
├── server/
│   ├── api/
│   │   └── counter.ts
└── app.vue
├── nuxt.config.ts
├── package.json
└── tsconfig.json

Create a server API for sending your events:

// ~/server/api/counter.ts
export default defineEventHandler(async (event) => {
	setHeader(event, "Content-Type", "text/event-stream");
	setHeader(event, "Cache-Control", "no-cache");
	setHeader(event, "Connection", "keep-alive");
	event.node.res.flushHeaders();

	// Increment count every second and send it as data
	let count = 0;
	const intervalId = setInterval(() => {
		let eventString = `event: message\n`;
		const timestamp = new Date().toISOString();
		eventString += `id: ${timestamp}\n`;
		eventString += `data: ${JSON.stringify(++count)}\n\n`;
		event.node.res.write(eventString);
	}, 1000);

	// Clean up on client disconnect
	event.node.req.on("close", () => {
		clearInterval(intervalId);
		event.node.res.end();
	});
});

Consume events from any page:

// ~/app.vue (or any page / component)
<script setup lang="ts">
import { useEventSource } from "@minch/use-event-source";

const { eventData, restartConnection, closeConnection, isStopped } =
	useEventSource("/api/counter", 0);
</script>

<template>
	<div>
		<h1>{{ eventData }}</h1>

		<p>The number is being sent from the server in real-time.</p>

		<button @click="restartConnection" :disabled="isStopped">Restart</button>

		<button @click="closeConnection" :disabled="!isStopped">Stop</button>
	</div>
</template>

Related Packages

sse-stream-plugin-vue

Vue 3 composables for sse-stream-plugin-sdk

196
nuxt-app-config-plus

Create nested App Config using a folder structure.

11,545 11
@x-app/nuxt-api

Configurable API composable for Nuxt 3 and Nuxt 4

393
nuxt-open-fetch

Generate zero-overhead, 100% typed OpenAPI clients for Nuxt.

379,588 259
@scayle/storefront-nuxt

Nuxt integration for the SCAYLE Commerce Engine and Storefront API

152,903

Version History

Version Released Vue License
1.3.20 ^3.0.0 MIT
1.3.19 ^3.0.0 MIT
1.3.18 ^3.0.0 MIT
1.3.15 ^3.0.0 MIT
1.3.13 ^3.0.0 MIT