It might not beat a bespoke hand-crafted protocol that minimizes message size, but your use of the word "negotiate" makes me think that that's not what you're taking about...
Essentially I'd like to inject a custom respond[0] encoder and a custom parseMessage[1] decoder, and same for the client.
[0]: https://github.com/trpc/trpc/blob/main/packages/server/src/a...
[1]: https://github.com/trpc/trpc/blob/main/packages/server/src/a...
Here's a gist with the wrappers (transporting the data as a JSON array instead of a dict): https://gist.github.com/TimonLukas/7c757a3b234344ad71e6bd5a6...
It's not ideal insofar that I have to parse the stringified JSON again. Some kind of real "encoder/decoder" parameter could be an amazing help.
This will reduce message size quite a bit, since the data isn't sent as:
{ "id": 3, "method": "subscription.stop" }
but instead as: [3,1]I will add wrappers for WebSocket/WebSocketServer in the future to allow using this without the library having to support it, like in the PoC with trpc.