-
-
Save didavid61202/99fb167cc7edc581e6bf376308e33bf1 to your computer and use it in GitHub Desktop.
SSE endpoint example with Nuxt 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
await new Promise((resolve) => { | |
// Wait forever | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment