Closed pintomau closed 8 months ago
Hey, looks like it should indeed work as you expect... Could you inspect the SSE request's event stream in your browser's developer tools (network tab)? Is everything normal in there, do you see events and data as they should look like?
Hey @Telroshan , the following is captured:
And the captured object:
using:
<script>
document.body.addEventListener('htmx:sseMessage', (evt) => {
console.log(evt)
})
</script>
Here, I have to change swap="message" so that the sseMessage
event is triggered.
Please provide version information about htmx and sse.js, I'll take a look at it
This is how the test server constructs SSE message
Hi @Renerick . Using the latest (?) versions:
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<script src="https://unpkg.com/htmx.org@1.9.10/dist/htmx.min.js"></script>
<script src="https://unpkg.com/htmx.org@1.9.10/dist/ext/sse.js"></script>
</head>
<body>
<div hx-ext="sse" sse-connect="/sales/subscribe/ad728360-a21d-4e8d-ab59-703446c0d356" sse-swap="message"></div>
<script>
document.body.addEventListener('htmx:sseMessage', (evt) => {
console.log(evt)
})
</script>
</body>
</html>
Ok versions seem fine
I modified the test server to use your example
const event = "event: Wololo\ndata: <div>Swapped</div>\n\n"
It works. Looking at the screenshot, it appears that you were debugging with Firefox. This is what I saw when trying the test server with code above in Firefox
Comparing to your screenshot, it looks like the data in your case is not processed correctly, your "Raw Data" view shows event:
and data:
, whereas in my case I only see HTML content from data
.
I think the issue is on the backend. What library do you have for SSE? My suspicion is that the function you use encodes the string you provided as data
instead of writing it to response as is. I also expect there to be a method (or method overload) that accepts event name as argument so you don't need to manually construct SSE string.
Probably looks something like
sseSubscriberRegistrar.sendEvent(userId, "Wololo", eventBody)
Indeed. I dug deeper, and it seems like it's how Spring's SSE Emitter works.
It forces data instead of treating as plain.
For those hitting the same issue, use SseEventBuilder as such:
emitter.send(SseEmitter.event().name("Wololo").data("<div>Swapped</div>"))
Thanks for the support.
Hey.
Following the guide in https://htmx.org/extensions/server-sent-events/#receiving-named-events, I'm trying to send named events:
Tried multiple variants such as "\n\n" at the end and so on...
In the frontend part I have:
However, I always get the
message
type instead of the one properly defined and documented.Can you please clarify what exactly is the expected format for specific message types?
Thank you.