Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Usage example for SSE in the docs #2374

Open
1 task
martinszeltins opened this issue Apr 17, 2024 · 6 comments
Open
1 task

Add a Usage example for SSE in the docs #2374

martinszeltins opened this issue Apr 17, 2024 · 6 comments
Labels
documentation Improvements or additions to documentation

Comments

@martinszeltins
Copy link
Contributor

Describe the feature

Currently in the docs there is a Usage example for how to create a WebSocket Nitro route: https://nitro.unjs.io/guide/websocket#usage but there is no example for server-sent events and how to create a Nuxt/Nitro route for that. There is an example in h3 docs but not in Nitro.

It would be very useful to create such usage example also for SSE (Server-sent events) just like WebSockets example.

Create a websocket handler in routes/_ws.ts

export default defineWebSocketHandler({
  open(peer) { ... },
  message(peer, message) { ... },
  close(peer, event) { ... },
})

Additional information

  • Would you be willing to help implement this feature?
@martinszeltins
Copy link
Contributor Author

@pi0 This is what we talked about in this gist.

@pi0
Copy link
Member

pi0 commented Apr 17, 2024

Thanks for the issue ❤️ Feel free to contribute to the docs by making a PR.

@pi0 pi0 added documentation Improvements or additions to documentation and removed pending triage labels Apr 17, 2024
@martinszeltins
Copy link
Contributor Author

martinszeltins commented Apr 17, 2024

Thanks for the issue ❤️ Feel free to contribute to the docs by making a PR.

@pi0 To be honest, I am not entirely sure what the usage should be. You mentioned that the usage is the same as in h3 but in a Nuxt/Nitro server route there is usually some kind of an event handler like in the example of @Atinux SSE endpoint example with Nuxt 3 gist

// ~/server/api/sse.ts
export default defineEventHandler(async (event) => {
  // ...
})

And then the client can connect to the route endpoint

// client code
const sse = new EventSource('/api/sse')

@pi0
Copy link
Member

pi0 commented Apr 18, 2024

Simply like this https://stackblitz.com/edit/github-lku74j?file=server%2Froutes%2Findex.ts

And you can either render it directly or connect with an eventsource client.

you can also use useNitroApp().hooks to use global hooks but i guess for sake of example we probably don't want to complicate it.

@martinszeltins
Copy link
Contributor Author

martinszeltins commented Apr 18, 2024

Simply like this stackblitz.com/edit/github-lku74j?file=server%2Froutes%2Findex.ts

And you can either render it directly or connect with an eventsource client.

you can also use useNitroApp().hooks to use global hooks but i guess for sake of example we probably don't want to complicate it.

Works like a charm, tested also with Nuxt. I will send in pull request to update the docs.

Pull Request: #2379

@SelfhostedPro
Copy link

Have an example of SSE working here in a real world app if that's beneficial at all:

https://github.com/SelfhostedPro/yacht-nuxt/blob/main/modules/notifications/runtime/server/composables/sse.ts

Has some stuff around Auth too and creating unique sessions for if you don't want all the events on every SSE connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants