Skip to content

Conversation

@RenameMe1
Copy link
Contributor

@RenameMe1 RenameMe1 commented Apr 9, 2025

Description

I would like to propose a solution for issue #4082.

  • Created new classes ASGIStreamingSSEResponse which supported sending ping messages

Closes

#4082

@codecov
Copy link

codecov bot commented Apr 9, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 4 lines in your changes missing coverage. Please review.

Project coverage is 98.36%. Comparing base (a98d958) to head (165dd59).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
litestar/response/sse.py 92.30% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4098      +/-   ##
==========================================
- Coverage   98.37%   98.36%   -0.01%     
==========================================
  Files         348      348              
  Lines       15890    15944      +54     
  Branches     1755     1762       +7     
==========================================
+ Hits        15631    15683      +52     
- Misses        122      123       +1     
- Partials      137      138       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@RenameMe1
Copy link
Contributor Author

RenameMe1 commented Apr 9, 2025

@sobolevn can you help me please with litestar/response/streaming.py:154: error: in mypy.

I don't follow why this an error occur.

@provinzkraut
Copy link
Member

This should not be named timeout as it is not a timeout.

Copy link
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest a different design. Take a look at how the stream handles client disconnects. Ideally, the two background tasks can share the same task group. Otherwise, there's unnecessary overhead involved, and cancellations become quite complex

@RenameMe1 RenameMe1 force-pushed the sse_timeout_events branch from a998181 to 2c7fdd9 Compare April 10, 2025 11:47
@RenameMe1 RenameMe1 requested a review from provinzkraut April 10, 2025 11:58
@RenameMe1 RenameMe1 changed the title feat: sse timeout events feat: sse ping_interval events Apr 10, 2025
Copy link
Contributor

@euri10 euri10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some minor comments.
We'd need tests for the missing coverage and to test the functionnality too but this looks good overall!

@RenameMe1
Copy link
Contributor Author

I added some minor comments. We'd need tests for the missing coverage and to test the functionnality too but this looks good overall!

I have read all the SSE tests and don't understand how I can test it. Can you help me, please?

I thought I would create an example with ping_interval and test it.

@RenameMe1 RenameMe1 marked this pull request as draft May 11, 2025 08:25
@RenameMe1 RenameMe1 force-pushed the sse_timeout_events branch 2 times, most recently from dff5816 to 174bc0f Compare May 11, 2025 09:13
@RenameMe1 RenameMe1 marked this pull request as ready for review May 11, 2025 09:17
@RenameMe1
Copy link
Contributor Author

@provinzkraut, friendly ping. Can you review, please?

I'm moved out work on ping messages to another class ASGIStreamingSSERespnse. Additionaly added state to event generator

@euri10
Copy link
Contributor

euri10 commented May 11, 2025

I didnt check in details thechanges but one thing I'd like to see if you dont mind is a little addition in the docs for that feature: https://docs.litestar.dev/latest/usage/responses.html#server-sent-event-responses

something like that below this would do the job:

In your iterator function you can yield integers, strings or bytes, the message sent in that case will have message as the event_type if the ServerSentEvent has no event_type set, otherwise it will use the event_type specified, and the data will be the yielded value.

If ping_interval is set to a positive value, blablabla. It is useful for instance on Telegram blablabla, (see the issue)

Copy link
Member

@provinzkraut provinzkraut left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @RenameMe1, unfortunately this still doesn't look just right.

The main issue is the separation of concerns here.
We do have a ASGIStreamingResponse. It should not care about SSE specifics. To implement SSE specific functionality, you'll have to implement it on a separate response class (which you did with ASGIStreamingSSEResponse), and then use that when returning an SSE stream.

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4098

@RenameMe1
Copy link
Contributor Author

Hello @provinzkraut, I'm moved out SSE implementation into litestar/response/see.py. Can you see, please?

Thanks for you help, and so sorry for mistakes ><

@RenameMe1
Copy link
Contributor Author

@provinzkraut, friendly ping. Can you review, please?

I hope, my work is right

@RenameMe1
Copy link
Contributor Author

@provinzkraut, friendly ping. Can you review, please?

Maybe it would be better to close this PR and let other people do it?

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

Labels

area/docs This PR involves changes to the documentation area/response pr/external size: small Triage Required 🏥 This requires triage type/feat

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants