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

Feature Request: Allow SSE extension to use a user-defined EventSource object #143

Open
adonespitogo opened this issue Feb 5, 2025 · 2 comments

Comments

@adonespitogo
Copy link

This will allow more control over the behavior of the SSE connection. For instance, I want to implement the fix for this firefox bug where I need to have access to the EventSource object for this to be implemented. This will also fix several issues:

Another advantage is to be able to add event listeners outside of htmx context.

Also, the number of connections is limited to only 6 when not used in HTTP2, so it would be beneficial to reused an existing connection across multiple independent elements.

Warning: When not used over HTTP/2, SSE suffers from a limitation to the maximum number of open connections, which can be especially painful when opening multiple tabs, as the limit is per browser and is set to a very low number (6). The issue has been marked as "Won't fix" in Chrome and Firefox. This limit is per browser + domain, which means that you can open 6 SSE connections across all of the tabs to www.example1.com and another 6 SSE connections to www.example2.com (per Stack Overflow). When using HTTP/2, the maximum number of simultaneous HTTP streams is negotiated between the server and the client (defaults to 100).

@Telroshan
Copy link
Collaborator

Hey, it's not documented (documentation improvement PRs are always welcome btw!), but you should already be able to do it ;

  • The extension creates an EventSource with a basic constructor call:

    function createEventSource(url) {
    return new EventSource(url, { withCredentials: true })
    }

  • Though, it calls it through a public API here:

    // set a function in the public API for creating new EventSource objects
    if (htmx.createEventSource == undefined) {
    htmx.createEventSource = createEventSource
    }

So, you could already override htmx.createEventSource with a function of your own, you simply need to make sure that your code executes before the SSE extension initializes

Hope this helps!

@adonespitogo
Copy link
Author

adonespitogo commented Feb 5, 2025

Hi, @Telroshan thank you for this. Although this works, I'd like to make a PR for this as this looks like a hack. I'd like to have something like:

function createEventSource() {
  return Promise.resolve(new EventSource("/sse-events"));
}
<div hx-ext="sse" sse-create="createEventSource" sse-swap="message">

Let me know if this is something that may get accepted.

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

No branches or pull requests

2 participants