nitely / nim-hyperx

Pure Nim http2 client and server 🖖
MIT License
29 stars 0 forks source link

Implement WebSockets #11

Open nitely opened 6 months ago

nitely commented 6 months ago

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 commented 6 months ago

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.

guest271314 commented 1 month ago

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.

nitely commented 1 month ago

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.

guest271314 commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

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.

nitely commented 1 month ago

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.

guest271314 commented 1 month ago

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 commented 1 month ago

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

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

Cheers.