Skip to content

Add heartbeat functionality to SseEmitter #33582

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

Closed

Conversation

Torres-09
Copy link
Contributor

This PR addresses issue #33355 by adding heartbeat (ping) functionality to the SseEmitter class.

Heartbeat Functionality
Added a new constructor to SseEmitter that accepts a heartbeatInterval parameter:

Heartbeat messages are sent as SSE comments (:heartbeat) at the specified interval.

Heartbeat messages automatically stop when the emitter is completed, times out, or encounters an error.

Testing
Added test cases to verify that heartbeat messages are sent at the correct intervals.

All tests have passed successfully.

Issue: #33355

This commit adds test cases to verify the heartbeat (ping) feature in SseEmitter. The tests ensure that heartbeat messages are sent at the specified interval and that they stop after the emitter is completed.

See: spring-projectsgh-33355
This commit introduces the heartbeat (ping) feature in SseEmitter. It allows the server to send periodic heartbeat messages to the client at a specified interval to keep the connection alive and detect client disconnects. Heartbeat messages are sent as comments (":heartbeat") and stop when the emitter is completed, times out, or encounters an error.

See: spring-projectsgh-33355
@Torres-09 Torres-09 changed the title Add heartbeat (ping) functionality to SseEmitter Add heartbeat functionality to SseEmitter Sep 23, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 23, 2024
@mdeinum
Copy link
Contributor

mdeinum commented Sep 24, 2024

I can see a risk here if many SseEmitters are created, each will create a Thread and take up memory and cpu cycles which can lead to memory and performance issues.

@Torres-09
Copy link
Contributor Author

@mdeinum, thank you for your review.

When I added the new heartbeat feature, my primary considerations were: (1) not to disrupt any existing functionality, and (2) not to introduce performance issues.

Unfortunately, it appears that the current changes do not fully meet the second condition. I overlooked the fact that creating an executor for each new instance could lead to performance problems due to the overhead of additional threads.

To address this, I am considering new approaches, such as (1) injecting a shared static resource, or (2) utilizing a TaskScheduler.

@jhoeller jhoeller added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 26, 2024
@bclozel
Copy link
Member

bclozel commented Sep 30, 2024

I'm declining this in favour of #33355 due to the concerns expressed here. Let's find another way to tackle this.

@bclozel bclozel closed this Sep 30, 2024
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants