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 onEvent callback to useEventSource #341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KubaJastrz
Copy link

@KubaJastrz KubaJastrz commented Apr 16, 2024

Adds a callback to event source stream, which fires whenever new data comes in. Useful if you don't want to merely display the latest message value, but rather do something with it. Like displaying a toast, computing a new state, analytics, you name it.

Usage:

useEventSource("/sse", {
	onEvent(data) {
		console.log(data);
	},
});

@sergiodxa
Copy link
Owner

This can easily be done with an effect in user-land:

let value = useEventSource("/sse");
useEffect(() => {
  // do something with `value`
}, [value])

@KubaJastrz
Copy link
Author

That's only if the value has changed. I suppose it could be worked around by adding some unique identifier inside of the message.

@KubaJastrz
Copy link
Author

KubaJastrz commented May 28, 2024

@sergiodxa are you interested in adding this change?

@bitofbreeze
Copy link

bitofbreeze commented Aug 9, 2024

@KubaJastrz @sergiodxa Could we extend useEventSource to allow reconnecting when the stream is closed? I don't see any way of currently doing this, and I think using a similar idea as here but making onEvent report disconnects too or adding perhaps an onError handler could make this possible.

For reference, here are the handlers partykit's websocket client hook exposes:
image

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.

3 participants