Skip to content

Instantly share code, notes, and snippets.

@Atinux
Last active June 4, 2024 05:47
Show Gist options
  • Save Atinux/05836469acca9649fa2b9e865df898a2 to your computer and use it in GitHub Desktop.
Save Atinux/05836469acca9649fa2b9e865df898a2 to your computer and use it in GitHub Desktop.
SSE endpoint example with Nuxt 3
// ~/server/api/sse.ts
export default defineEventHandler(async (event) => {
if (!process.dev) return { disabled: true }
// Enable SSE endpoint
setHeader(event, 'cache-control', 'no-cache')
setHeader(event, 'connection', 'keep-alive')
setHeader(event, 'content-type', 'text/event-stream')
setResponseStatus(event, 200)
let counter = 0
const sendEvent = (data: any) => {
event.node.res.write(`id: ${++counter}\n`);
event.node.res.write(`data: ${JSON.stringify(data)}\n\n`);
}
myHooks.hook('sse:event', sendEvent)
// Let the connection opened
event._handled = true;
})
@jd-solanki
Copy link

As @martinszeltins Says we shouldn't discuss it here. Let's not spam the gist here. Thanks for informing 🤝🏻

If anyone has something to say, I've seperate issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment