-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
upgradeWebSocket could return a WebSocketStream #14064
Comments
Yes, this is the plan longterm, and was actually the plan originally to some degree (for both statements, its rather not alongside, but instead of), however, the idea is that the server side and client side use the same API for a smoother experience, but since browsers do not have WebSocketStream yet, this isnt exactly the case |
I'm looking forward to this feature as well 😃 I have Deno programs as both the client and the server, so I get to do the nice W3C streams stuff on the client side, just to deal with the classic WebSocket callbacks on the server side. |
I discussed it with @crowlKats recently and the fact that there's still no spec makes it quite hard to ship in Deno. @crowlKats anything changed since we spoke about it? |
Sadly no, there has been no progress from what I am aware |
Could it make sense to, at least while |
Tried my hand at a PR taking some inspiration from, and trying to improve upon, the earlier PR (#16732) by @crowlKats. |
export class WebSocketStream {
public socket: WebSocket;
public readable: ReadableStream<Uint8Array>;
public writable: WritableStream<Uint8Array>;
constructor(socket: WebSocket) {
this.socket = socket;
this.readable = new ReadableStream({
start(controller) {
socket.onmessage = function ({ data }) {
// console.log(data);
return controller.enqueue(new Uint8Array(data));
};
socket.onerror = (e) => controller.error(e);
socket.onclose = (/* e */) => controller.close(/* e */);
},
cancel(/* reason */) {
socket.close();
},
});
this.writable = new WritableStream({
start(controller) {
socket.onerror = (e) => controller.error(e);
},
write(chunk) {
// console.log(chunk);
socket.send(chunk);
},
close() {
socket.close();
},
abort(e) {
console.error(e);
socket.close();
},
});
}
} |
One important feature of the WebSocketStream is the backpressure handling, and this simple wrapper does not support backpressure at all. It is possible to implement backpressure on the outgoing stream via |
Currently,
Deno.upgradeWebSocket
returns aWebSocket
. It could return aWebSocketStream
, too, for example:The text was updated successfully, but these errors were encountered: