Skip to content

SSE close connection in window.beforeunload #2109

Closed Answered by jonfriesen
jonfriesen asked this question in Q&A
Discussion options

You must be logged in to vote

I managed to resolve this myself with an addition to sse.js.

I added this function near the helpers.

function closeAllSSEConnections() {
  document.querySelectorAll('[sse-connect], [hx-sse]').forEach(function(elt) {
      var internalData = api.getInternalData(elt);
      if (internalData && internalData.sseEventSource) {
          internalData.sseEventSource.close();
      }
  });
}

And then attach it to the window on init

init: function(apiRef) {
	// store a reference to the internal API.
	api = apiRef;

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

	// (NEW) registe…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jonfriesen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant