Been playing around this weekend with the websocket protocol. It looks to me like you need to send invalid JSON to make this work. Now I’ve not found any docs at all and certainly no claims that it’s meant to be valid JSON so this isn’t a criticism at all, I’m just hoping to find someone that knows more who can confirm or point me at some docs?
Basically, the message field, the value is not a json string because it requires invalid json encoding - unescaped newlines, quotes etc. If you use a standard json encoder to prepare the html before passing it down the websocket, then at best I’m finding the turbo JS adding literal “\n” (i.e. 2 chars) etc all over the page.
Is this working as intended right now, or is there some special header I’ve failed to set or something?
@matthew it can be pretty confusing at first . Turbo just expects the server to send down nothing but HTML with the proper HTTP header and it will take care of the rest (although I have run into problems with HTTP requests that aren’t made by link clicking / form submissions).
So whatever framework you’re using you want to turn any JSON parsing / handling OFF on the server and the client.
Well I’ll be… So I’m working in Go, and a couple of examples I’ve found have been wrapping the message on websockets with stuff like this:
And so then the stuff after “message” is all non-json whilst the whole thing looks like it should be.
And in my own code, I’ve followed that weirdness and it all was working.
But, I’ve now removed the { and } wrapping, and the identifier key-value pair, and the message key and am just sending the html fragment directly, and it’s all working just great! So maybe that stuff originated in some older version or was based on some misunderstanding, but it certainly doesn’t seem to be required for me now - as you kinda suggested, just sending the HTML and nothing more seems to work fine. Many thanks!
I’ve finally managed to get both SSE and WebSocket messages to work, under node and EJS. Right now I have them in this webpack branch of this NPM’s example-app, but I am hoping I can add helper functions to make it easier.
The README explains a little bit the message format that seems to work.