Skip to content

Conversation

@daniel-bryant
Copy link
Contributor

See my original issue #56 for some background.

Currently the library only supports close events, which fire when the connection is closed from the client side. An additional event for tracking server side closing of the connection would be useful. This PR adds a done event to do that. Luckily most of the plumbing is already there, so it was easy to hook up the new event. As far as naming goes, I just matched the state name like most other events seem to do. It still might be a little confusing, so I added some text to the README.

README.md Outdated
### Done vs Close

`done` events will fire when server closes the connection.
Reconnections will occur indefinitely, unless this behavior is disabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should be rephrased slightly to make it more clear. Maybe something like:

By default, the client will automatically reconnect when this happens. You can disable the reconnections by setting the pollingInterval option to 0".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good to me. Updated.

this._handleEvent(xhr.responseText || '');

if (xhr.readyState === XMLHttpRequest.DONE) {
this.dispatch('done', { type: 'done' });
Copy link
Contributor

Choose a reason for hiding this comment

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

I would strongly recommend to dispatch the done event after calling _pollAgain. This would allow users to cancel the reconnection by calling .close() from the done handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. I just updated and tested that it works as expected.

This allows 'close' called from a 'done' handler to cancel the upcoming
reconnection. Otherwise '_pollTimer' will get reset after the handler.
@EmilJunker
Copy link
Contributor

LGTM 👍

@rafeautie
Copy link

any idea when this will merge? :)

@quentez
Copy link

quentez commented Jul 1, 2025

@daniel-bryant Thank you for the great work. I hope this gets merged eventually, but in the meantime it 100% solved the problem we were having 🙇‍♂️

@wojciechkrol wojciechkrol merged commit 97b7462 into binaryminds:master Jul 1, 2025
@daniel-bryant
Copy link
Contributor Author

@quentez that is great to hear, happy to help!

@amin-hashthink
Copy link

amin-hashthink commented Nov 22, 2025

it's not merged into the master branch (not available in the latest release, 1.2.1). how can I use this fix @daniel-bryant ?

@amin-hashthink
Copy link

amin-hashthink commented Nov 22, 2025

it's not merged into the master branch (not available in the latest release, 1.2.1). how can I use this fix @daniel-bryant ?

was more of a git/ package management question actually!
for anyone else with the same question, that can be done with a command like this which mentions the git repo and the exact commit (I chose the the latest commit @daniel-bryant added):
bun add react-native-sse@git+https://github.com/binaryminds/react-native-sse.git#6b59efd7b9f410e30bf029547db50581f9b810ae

but pls let me know if there's a better way!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants