D
Deno

help

Can someone explain WebSocketStream to me?

Mmokyu10/3/2023
Whenever I try to connect using WebSocketStream and the host doesn't respond, my program inevitably crashes and stops. I'd like to have the program attempt to reconnect without stopping even if an error occurs, but I can't seem to make it work. Does anyone have a solution for this? function webSocketStream_Connection() { try { const wss = new WebSocketStream("wss://localhost") } catch (err) { setTimeout(() => webSocketStream_Connection(), 5) } } webSocketStream_Connection() error: Uncaught (in promise) ConnectionRefused: 対象のコンピューターによって拒否されたため、接続できませんでした。 (os error 10061)
DAHDeno AI Helper10/3/2023
The WebSocketStream API is a modern WebSocket API that is based on web streams. It's a successor to the existing WebSocket API that is based on events. The new API solves a long-standing issue with the existing WebSocket API: backpressure. Events could be dispatched as fast as the browser wants, without giving the developer any way to pause event dispatch. This meant it is easy to overload the JS thread if you do some computation on every event. The new API solves this by using a ReadableStream as its receiving (readable) end, and a WritableStream as its sending (writable) end. Because a developer always has to “request” the next item from the stream, the browser can’t flood the script with events unasked. In your case, it seems like you're trying to establish a connection using WebSocketStream and if the connection fails, you want to retry the connection after a delay. However, the issue with your code is that you're not awaiting the connection to be established. The WebSocketStream constructor returns immediately, but the connection is established asynchronously. You need to await the wss.connection promise, which will resolve when the connection is established or reject if the connection cannot be established. Here's how you can modify your code:
async function webSocketStream_Connection() {
try {
const wss = new WebSocketStream("wss://localhost");
const { writable, readable } = await wss.connection;
// Connection established, you can now use `writable` and `readable`
} catch (err) {
console.error("Failed to connect, retrying in 5 seconds", err);
setTimeout(webSocketStream_Connection, 5000);
}
}
webSocketStream_Connection();
async function webSocketStream_Connection() {
try {
const wss = new WebSocketStream("wss://localhost");
const { writable, readable } = await wss.connection;
// Connection established, you can now use `writable` and `readable`
} catch (err) {
console.error("Failed to connect, retrying in 5 seconds", err);
setTimeout(webSocketStream_Connection, 5000);
}
}
webSocketStream_Connection();
In this code, if the connection cannot be established (for example, if the
DAHDeno AI Helper10/3/2023
server is not running), the wss.connection promise will reject, and the error will be caught in the catch block. The function then schedules itself to be called again in 5 seconds, effectively retrying the connection. Please note that WebSocketStream is still experimental and may change in the future. You need to use the --unstable flag to use it.
Mmokyu10/3/2023
I've tried the following code: However, I'm getting an error and the program stops before it retries the connection.
async function webSocketStream_Connection() {
try {
const wss = new WebSocketStream("wss://localhost");
const { writable, readable } = await wss.connection;
// Connection established, you can now use `writable` and `readable`
} catch (err) {
console.error("Failed to connect, retrying in 5 seconds", err);
setTimeout(webSocketStream_Connection, 5000);
}
}
webSocketStream_Connection();
async function webSocketStream_Connection() {
try {
const wss = new WebSocketStream("wss://localhost");
const { writable, readable } = await wss.connection;
// Connection established, you can now use `writable` and `readable`
} catch (err) {
console.error("Failed to connect, retrying in 5 seconds", err);
setTimeout(webSocketStream_Connection, 5000);
}
}
webSocketStream_Connection();

Looking for more? Join the community!