Skip to content

Instantly share code, notes, and snippets.

@Atinux
Last active April 22, 2024 20:39
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

Regardless of the given example, I would like to ask question on,

How can I consume SSE on Nuxt client? Is there any simple API like this.$sse.on('tweet', (tweet) => this.tweets.unshift(tweet)) mentioned here.

Also, there can be cases where we've to make a POST req that responds with SSE and it might include JSON and not just plain string.

@martinszeltins
Copy link

Regardless of the given example, I would like to ask question on,

How can I consume SSE on Nuxt client? Is there any simple API like this.$sse.on('tweet', (tweet) => this.tweets.unshift(tweet)) mentioned here.

Also, there can be cases where we've to make a POST req that responds with SSE and it might include JSON and not just plain string.

@jd-solanki you can use the useEventSource from vueuse library - https://vueuse.org/core/useEventSource/

@jd-solanki
Copy link

Hi @martinszeltins

Yes, I can but it doesn't support POST request. I'm making POST request and in response, I'm getting SSE response.

@masyoudi
Copy link

Hi @martinszeltins

Yes, I can but it doesn't support POST request. I'm making POST request and in response, I'm getting SSE response.

i think you need event emitter

@jd-solanki
Copy link

@masyoudi Sorry I didn't get what you mean? What's Event Emitter related to SSE? Any example you can provide?

@martinszeltins
Copy link

Hi @martinszeltins

Yes, I can but it doesn't support POST request. I'm making POST request and in response, I'm getting SSE response.

@jd-solanki I think we're spamming this gist with notifications. But to answer your question, you do not make POST requests to SSE endpoint. SSE is for receiving events from the server. If you want to send data to the backend, you would use a different endpoint (graphql, rest etc.). And then whenever the backend has something new to say, it will stream a message to the client using the established SSE/EventSource connection. Feel free to provide a code example of what you're trying to do.

@masyoudi
Copy link

there is no relation between SSE with Event Emitter

as you can see SSE only aplicable on request method GET, so when you need to trigger the EventSource from request method POST, you need different approach

so in my opinion, i need to create function outside SSE route that can be trigger the EventSource, in this case i can use Event Emitter / hookable, and then inside POST route i can trigger the Event Emitter

i have a simple example in here

@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