Closed Nairda015 closed 1 year ago
So I found workaround for now you can return message like this with load trigger
var message = $"""<div class="hidden" hx-get="/refresh-board" hx-trigger="load" hx-swap="outerHTML" hx-target="#board"></div>""";
await hubContext.Clients
.Group(log.GameId.Value)
.SendAsync("game-started", message, ct);
it is important to add empty subscriber
<div id="game" hx-ext="signalr" signalr-connect="/game-hub">
...
<div id="new-game-hack" signalr-subscribe="game-started"></div>
</div>
Server-Sent Events are a separate technology and htmx extension, independent of WebSockets and SignalR extensions. The feature you highlighted (using sse:
prefixed events specifically) is only supported by SSE extension and won't work with WS or SignalR extensions.
The workaround you provided is necessitated by the design of SignalR itself, that is, you have to explicitly subscribe to desired events ^1 and by the goal of mirroring the way htmx's official WebSocket extension works by receiving HTML strings. Ideally, you would be able to pass HX-Trigger header in the message from the server, but it's not possible due to this design constraints atm.
If you don't want to pass unnecessary html in the message, you should be able to utilize this extension's events, but you would have to use signalr-subscribe
attribute regardless
<div id="new-game-hack" signalr-subscribe="game-started" hx-get="/refresh-board" hx-trigger="htmx:singnalr:message" hx-swap="outerHTML" hx-target="#board"></div>
I'm going to close the issue as there is nothing to fix really. Feel free to reopen if you have any other questions or suggestions
Maybe I don't understand this fully. Is this possible to send notification without message only to trigger get request from the client?
await hubContext.Clients.All.SendAsync("lobby-updated", stoppingToken);
<tbody
id="lobby-table-body"
signalr-subscribe="lobby-updated"
hx-get="/refresh-lobby"
hx-trigger="load, htmx:singnalr:message">
</tbody>
hmm, this should work
you have a typo
- htmx:singnalr:message
+ htmx:signalr:message
Thanks!
In the code there is no check if message is empty and internally htmx throw so maybe we can add
if (!message) return;
around line 203
Agree, I'll do that in a bit
What I'm doing wrong? Hub is mapped and I can confirm that messages from the websocket are received in dev tools
https://htmx.org/extensions/server-sent-events/