-
Notifications
You must be signed in to change notification settings - Fork 56
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
No way to proxy SSE data #48
Comments
Hey, doesn't the htmx-extensions/src/sse/sse.js Lines 118 to 123 in 719538f
Let me know! |
Thanks for the response @Telroshan. I'm afraid The shortest workaround I've found is to fork the library and dispatch a custom event with a closure like so: var decodedData = null
const setDecodedData = function(data) {
decodedData = data
}
const newEvent = new CustomEvent('htmx:sseBeforeSwap', {
detail: { setDecodedData, data: event.data },
})
// swap the response into the DOM and trigger a notification
if (!api.triggerEvent(elt, 'htmx:sseBeforeSwap', newEvent)) {
return
}
swap(elt, decodedData || event.data) Which is no easier than just decoding directly in the extension side. |
Oh, indeed the properties are read-only... Just some thoughts out loud, if you feel like it, feel free to explore solutions and submit a PR! |
I would be happy to contribute. Approaching this problem from another perspective, I think message encoding should be configured to a specific SSE connection/endpoint. I would go for an <div hx-ext="sse" sse-connect="/api/events" sse-encoding="base64">
<!-- html fowarded to swaps is already decoded into text -->
</div> Then the proper encoding process would occur inside a callback registered inside |
This would solve your immediate usecase, but I'im thinking an event-based solution would be more modular and let people handle more specific usecases as needed. I would suppose that the |
Running into the same issues here. Both the encoding issue but also the lack of ability to modify the event data before it is swapped in. The workaround sofar is to just use hx-trigger with sse and do a round trip to the server with hx-get to get the properly formatted data. This reduces the functionality of sse to mere triggers. Also, as the formatting is a client side concern, having to code this on the server for each case where it differs is not the most efficient way. My latest workaround is to listen for the event htmx:sseBeforeMessage, cancel it with preventDefault, json decode the data field and use it to modify the event target directly in javascript. |
Any news on that? Is it possible to transform the event data before swap? Can't seem to get it working, either. |
It would be interesting if there was a way to update the server side event's data before they got swapped into the elements.
The main reason for this is that SSE specification uses
\n\n
as an event terminator and the way to send multiline data is to base64 encode it before. However, there is no way to decode it on the frontend before the swap. At least not to my knowledge.The text was updated successfully, but these errors were encountered: