lustre-labs / lustre

A Gleam web framework for building HTML templates, single page applications, and real-time server components.
https://hexdocs.pm/lustre
MIT License
950 stars 65 forks source link

✨ Add support for HTTP server-sent events in Lustre Server Components. #42

Closed hayleigh-dot-dev closed 2 months ago

kljensen commented 3 months ago

Ahoy, I'm quite keen on this, though a lustre noob. Is there a realistic path to a noob contributing to this feature?

hayleigh-dot-dev commented 3 months ago

Enthusiastic noobs are welcome! how familiar are you with handling SSE in js in general?

kljensen commented 2 months ago

@hayleigh-dot-dev decently familiar? I've used SSE in angular, elm, and vanilla js with backends in elixir, go, node, and python. That said, it's been a bit.

hayleigh-dot-dev commented 2 months ago

I think the main thing would be just adding a second attribute to the server-component.mjs for mode=“‘ws’ | ‘sse’” and then copying the ws implementation for handling incoming events. I think the main difference is the SSE version would probably only receive events and not emit any?

kljensen commented 2 months ago

Thanks for the pointer. I looked at the code in that file and it seems to me that this is the Lustre analog of LiveView, is that right?

I suppose what I am interested in is not communicating DOM updates via SSE, but rather model updates. (I thought I'd create a RAG app to learn Lustre. And, to my naïve mind, I cannot see the value in SSE over WS for DOM updates.) Reading more through the docs, it seems like I likely want to be looking at the "custom effects". Is that right?

hayleigh-dot-dev commented 2 months ago

this is the Lustre analog of LiveView, is that right?

Yeah thats right.

but rather model updates.

In that case you'll write a custom effect thats right. You'll need to write the FFI code to work with fetch yourself (there's a community package called lustre_http but it doesn't support SSE as far as I'm aware) and you'd dispatch a message on every event.

I'll close this for now, if you run into some questions feel free to drop into lustre's discord, ask in the gleam one, or open an issue here ^.^

kljensen commented 2 months ago

Much thanks @hayleigh-dot-dev for your advice and also lustre.