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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate SSE support and add page aboutit to the documentation #6

Open
paveldedik opened this issue Mar 14, 2024 · 7 comments
Open
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@paveldedik
Copy link
Owner

No description provided.

@paveldedik paveldedik added documentation Improvements or additions to documentation enhancement New feature or request labels Mar 14, 2024
@johanfforsberg
Copy link

I tried SSE using sse-starlette and hx_sse and it works fine as far as I can tell! Do you intend to integrate it further than that?

hx-sse will go away in HTMX 2.0 though, so some changes will be needed: https://htmx.org/extensions/server-sent-events/

@paveldedik
Copy link
Owner Author

paveldedik commented May 26, 2024

Thank you very much for testing it. I'm glad to hear that it works! I would like to integrate it further. I'll soon create a roadmap and this will be close to the top (I want to finish new version of documentation first, it will be done soon). I noticed HTMX 2.0 and yeah, I definitely want to figure out how to add proper support in Ludic. I'm not sure how the API will be designed though.

@johanfforsberg
Copy link

Interesting! It was a little fiddly; adding the right HTMX attributes in the correct places, and creating SSE endpoints. But on the other hand it's flexible, and I found that I could just send Ludic html in events (guess due to it having a string representation). Probably some common usage patterns could benefit from more integration.

@paveldedik
Copy link
Owner Author

Could you share the code you wrote? I'm curious, and I agree, common usage patterns are a good idea.

@johanfforsberg
Copy link

Sure, here's a minimal example:

import asyncio

import ludic.html as h
from ludic.web import LudicApp
from sse_starlette.sse import EventSourceResponse, ServerSentEvent


app = LudicApp(debug=True)


@app.get("/")
async def index() -> h:
    return h.html(
        h.body(
            h.div(hx_sse="connect:/events swap:message"),
            h.script(src="https://unpkg.com/htmx.org@1.9.12"),
        ),
    )


async def count():
    value = 0
    while True:
        yield ServerSentEvent(data=value)
        value += 1
        await asyncio.sleep(1)


async def sse():
    return EventSourceResponse(count())


app.add_route("/events", sse)

@paveldedik
Copy link
Owner Author

Thank you very much. That is a nice sample, with a bit of testing from my side I'll probably add that to the documentation signed with you as the author of the commit if you don't mind. I also want to check the SSE support in HTMX 2.0 first.

@johanfforsberg
Copy link

My pleasure!

I found that I had overcomplicated my use case and probably I should stick with something quite similar to this example in the end. As far as I understand it, with HTTP2 all SSE connections will be "multiplexed" over one connection anyway, so there's little point to trying to collect several subscriptions into one, which was what made it complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants