-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Server-Sent Events implementation needs improvements #12670
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
Comments
This is completely fine and recommended when you need full control over the execution flow. |
could this be an example in the samples repo? |
Definitely! Would you like to add an example to the existing sse sample project? |
I was hoping the author would, since they wrote the code. I'm currently happy with the decorator. |
@Totalus , I can't thank you enough for sharing this |
In the era of large models, is it worth considering allowing SSE to support the HTTP POST method? @kamilmysliwiec ![]() |
Server-Sent Events implementation in Nest throught the
@Sse
decorator has flaws that makes it hard to use. I’ve created this issue to highlight the flaws and to also share a workaround to implement SSE without the decorator’s issues.That being said, feel free to comment. Maybe there are some good reasons I am not aware of why the
@Sse
decorator behaves like it does, or options I am not aware of.Problems with the decorator
When using
@Sse
decorator, the connection with the client is established before the handler is called (wether your handler is async or not according to my observations, see also #12260). As a result, throwing an HttpException inside the handler will not return an HTTP error as you would expect, but instead will send an SSE error type message, with the data of this message being the error name. Any additional data you might pass (ex: error code, etc.) when throwing the exception is lost. This behavior makes it harder to deal on the client side as you don’t know until after the connection is established if the call actually failed or not and you don't get the error details.Also, an empty SSE message is always sent just after a connection is established, not sure why that is and if it is intended or not.
Expectation
As a user, I want my endpoint to return an HTTP error code if the request is invalid for a reason or another and only establish the SSE connection if prior validation is complete without error. I also want my additional error data to be forwarded to my client so it can properly handle the error.
Documentation lacking details
I find the documentation page for SSE lacks some important details regarding how the connection is established before the handler is called and how the error handling works. Also, the provided example generates the data inside the request handler directly. In practice, chances are you want to establish a SSE connection to notify your clients of events that will happen in the future and are generated outside of the request handler. Finding out how to achieve that took more reaseach and brought me out of NestJS documentation. It would be nice to have those details directly in Nest's SSE documentation page.
Workaround
Because of the various issues, I took a step aside and implemented SSE without using the
@Sse
decorator which turned out to be fairly easy and provides much more control over the execution flow. A quick search made me realize I could directly used expressresponse
object to implement this in a normal@Get
request.In the background, SSE is just an HTTP connection that is kept alive in which you write messages as plain text following a standard format. All you need to establish the connection is to set the righ headers:
Here’s a complete example in which I am using observables, which makes it very close to what you would have in an
@Sse
decorated endpoint.The text was updated successfully, but these errors were encountered: