3 ms·
You can turn websockets into a flawless request/response with async/await included on like 20 lines of JavaScript. I do it all the time. Generate an ID, make a
by Philip-J-Fry 5y ago
You can turn websockets into a flawless request/response with async/await included on like 20 lines of JavaScript. I do it all the time.
Generate an ID, make a request, store the promise resolve/reject in a map (js object). Your onmessage handler looks up the promise based on the ID and resolves the promise.
Add a few more tiny features like messaging and broadcasting streams and you've got both request/response and push messaging over a single websocket.
It's pretty neat in my opinion, saves having to mix HTTP and websockets for a lot of things.
- anderspitman 5y agoIt is neat. I've gone down this path[0]. But you find yourself essentially re-implementing HTTP, and losing things like backpressure. Sometimes it's what you need, but I try to be cautious before jumping to WS these days. [0]: https://iobio.io/2019/06/12/introducing-fibridge/ https://iobio.io/2019/06/12/introducing-fibridge/