nim-hyperx icon indicating copy to clipboard operation
nim-hyperx copied to clipboard

Implement WebSockets

Open nitely opened this issue 1 year ago • 9 comments

Not sure how useful this is, WS over http2 seems supported by firefox and chrome at least.

For the hyperx client/server is kinda pointless, http2 supports sending any number of data frames back and forth over a single stream.

nitely avatar May 29 '24 19:05 nitely

There is also fetch streaming on browsers but for some reason they restricted it to half duplex, you either stream the request data or the response data, but not both. If full duplex is ever supported, it could replace websocket use cases. See https://github.com/whatwg/fetch/issues/1254

Well, half-duplex does allow to create a stream for receiving and one for sending, but then the server needs a way to associate both streams, like some sort of ID and possibly an external pub/sub, which makes things a lot more complex than they should be.

nitely avatar May 29 '24 19:05 nitely

There is also fetch streaming on browsers but for some reason they restricted it to half duplex, you either stream the request data or the response data, but not both.

Technically on Chromium based browsers it is possible to full-duplex stream between a ServiceWorker and a WindowClient or Client. See https://plnkr.co/plunk/2PQK21kZTZvZ2oVi.

Well, half-duplex does allow to create a stream for receiving and one for sending, but then the server needs a way to associate both streams, like some sort of ID and possibly an external pub/sub, which makes things a lot more complex than they should be.

Not sure what is meant here. In the server, if the client uploads a ReadableStream you can process that stream and send a ReadableStream to the client using the same connection.

guest271314 avatar Oct 20 '24 16:10 guest271314

That seems half-duplex. It does not matter that you can create a js stream out of a request/response. Full duplex allows to receive/send in a ping-pong way over one single stream, that's not supported in anyway other than websockets, AFAIK.

nitely avatar Oct 20 '24 16:10 nitely

Full duplex allows to receive/send in a ping-pong way over one single stream

That's exactly what is happening in the linked plnkr.

guest271314 avatar Oct 20 '24 19:10 guest271314

Full duplex allows to receive/send in a ping-pong way over one single stream, that's not supported in anyway other than websockets, AFAIK.

Both Node.js and Deno support full-duplex streaming using WHATWG Fetch. The only case in the browser where that is possible that I am aware of is between a ServiceWorker and a WindowClient or Client, in Chromium-based browsers (Chromium, Chrome, Brave, Edge, Opera).

guest271314 avatar Oct 20 '24 19:10 guest271314

that's not supported in anyway other than websockets, AFAIK.

Yes, WebSocket and WebSocketStream support that. So does WICG Direct Sockets TCPSocket and TCPServerSocket, which are both exposed in Chromium within an Isolated Web App. There are ways to implement sending and receiving those streams to any arbitrary window.

guest271314 avatar Oct 20 '24 19:10 guest271314

I don't see how this is relevant to this issue, but good to know ig.

That's exactly what is happening in the linked plnkr.

my bad, I only read the code at first, and it's kinda iffy, but it seems to work as you say.

nitely avatar Oct 20 '24 20:10 nitely

I think you mentioned something about streaming in a Bun issue. My machine crashed in the interim, I'm running on RAM only, so I don't have the exactly link I followed. In general I'm interested in WHATWG Streams, WHATWG Fetch, standard streams, media streaming, and streaming in general is probably why I commented here.

guest271314 avatar Oct 20 '24 20:10 guest271314

FWIW Here's how I control Node.js and Deno HTTP/2 streams over fetch() from the browser

  • https://github.com/guest271314/native-messaging-nodejs/tree/full-duplex
  • https://github.com/guest271314/native-messaging-deno/tree/fetch-duplex

Here's a JavaScript runtime agnostic WebSocket server run from Chromium - where we can keep a single WebSocketStream connection established and open indefinitely

  • https://github.com/guest271314/direct-sockets-http-ws-server

Cheers.

guest271314 avatar Oct 20 '24 20:10 guest271314