Evite que su aplicación se ahogue en mensajes de WebSocket o inunde un serverLos servidores son ordenadores centrales y potentes dentro del campo de la tecnología de la información que procesan y proporcionan software y archivos en una red. Desde el punto de vista de un cliente, varios ordenadores en una red pueden ponerse en contacto con el servidor central para conseguir la información solicitada. En la arquitectura cliente-servidor, el servidor puede ser un software que proporciona un servicio y se ejecuta en plus de WebSocket con mensajes aplicando contrapresión.

Background

The APIConcepto de API¿Qué es una API?Una API, siglas de Application Programming Interface o Interfaz de Programación de Apps, es un recopilatorio de código que se puede emplear para que varias apps se comuniquen entre ellas. Es algo que realiza una tarea equivalente a la interfaz de usuario al momento de promover la interacción entre persona y programa, solo que aplicado única y exclusivamente dentro del entorno del software.Aún cuando suene plus de WebSocket

the WebSocket API
proporciona una interfaz JavaScriptJavaScript es un lenguaje de programación que funciona en el lado del cliente y con el que las webs pueden ser más funcionales. Incorporación en código HTML El código JavaScript puede ser incrustado en las páginas HTML, para que adquieran funcionalidad. Existen varias opciones. Puede estar entre las etiquetas <SCRIPT> y </SCRIPT>, puede estar contenido en un archivo externo, puede ser un parámetro de las etiquetas HTML, y puede estar plus for WebSocket protocol, lo que permite abrir una sesión de comunicación interactiva bidireccional entre el browserUn navegador (además: browser) es una herramienta informática que te permite ver documentos y datos y navegar por la red. Los navegadores pueden mostrar distintos tipos de recursos de información; principalmente documentos HTML, a pesar de todo, además son posibles otros tipos de archivos y contenido multimedia, como PDF, JPEG, MPEG, GIF o el lenguaje de meta marcado. A través el uso de complementos especiales y la configuración respectivo, los plus del usuario y un servidor. Con esta API, puede enviar mensajes a un servidor y recibir respuestas basadas en eventos sin sondear el servidor para obtener una respuesta.

The Streams API

the Streams API
permite que JavaScript acceda mediante programmingConcepto de Programación¿Qué es la Programación?La Programación es el procedimiento al que se recurre para crear algún tipo de aplicación o software, para materializar un concepto o proyecto que requiere de la utilización de un lenguaje informático para poder llevarse a cabo. Es algo que está absolutamente ligado a la figura del programador, y que cada vez está cobrando más relevancia dentro del mundo del marketing.Decimos que tanto esta figura plus a flujos de datos recibidos a través de la red y los procese como desee. Un concepto importante en el contexto de las corrientes es
back pressure. This is the process by which a single stream or chain of pipes regulates the speed of reading or writing. When the transmission itself or a subsequent transmission in the pipeline is still busy and not yet ready to accept more fragments, it sends a signal back through the chain to slow delivery accordingly.

The problem with the current WebSocket API

Applying back pressure to received messages is impossible

With the current WebSocket API, the reaction to a message happens in
WebSocket.onmessage, a EventHandler called when a message is received from the server.

Suppose you have an application that needs to perform extensive data processing operations each time a new message is received. I would probably set up the flow similar to the code below, and since await the result of the process () call, you should be fine right?


const process = async (data) => {
return new Promise((resolve) => {
window.setTimeout(() => {
console.log('WebSocket message processed:', data);
return resolve('done');
}, 1000);
});
};

webSocket.onmessage = async (event) => {
const data = event.data;
await process(data);
};

Incorrect! The problem with the current WebSocket API is that there is no way to apply back pressure. When messages come faster than the process () The method can handle them, the rendering process will fill the memory by buffering those messages, it will stop responding due to CPU usage at 100%, or both.

Applying back pressure to sent messages is not ergonomic

It is possible to apply back pressure to sent messages, but it does involve polling the
WebSocket.bufferedAmount

property, which is inefficient and not ergonomic. This read-only property returns the number of bytes of data that have been queued by calls to
WebSocket.send (), but not yet transmitted to the network. This value is reset to zero after all the queued data has been sent, but if you keep calling WebSocket.send (), it will continue to rise.

What is the WebSocketStream API?

The WebSocketStream API addresses the problem of non-existent or non-ergonomic back pressure by integrating streams with the WebSocket API. This means that back pressure can be applied "free", at no additional cost.

Suggested Use Cases for the WebSocketStream API

Examples of sites that can use this API include:

  • High bandwidth WebSocket applications that need to maintain interactivity, in particular video and screen sharing.
  • Similarly, video capture and other applications that generate a large amount of data in the browser that must be uploaded to the server. With back pressure, the client can stop producing data instead of accumulating data in memory.

Actual state

He passed Condition
1. Create an explainer To complete
2. Create initial draft specification In progress
3. Collect feedback and repeat the design In progress
4. Proof of origin To complete
5. Launch Not started

How to use the WebSocketStream API

Introductory example

The WebSocketStream API is based on promise, which makes dealing with it feel natural in a modern JavaScript world. Start by building a new WebSocketStream y pasándole la UrlEl URL (Localizador Uniforme de Recursos), es una dirección definida que apunta a la posición de un archivo en un servidor y lo recupera. Las URL se introducen en un navegador web para ingresar a documentos en la web o se incrustan como hipervínculos dentro de un documento. Se puede usar un Permalink para que una URL esté disponible de forma permanente. Componentes de una URL • prefijo de protocolo plus del servidor WebSocket. A continuación, espere el connection established, resulting in a
ReadableStream

and / or a
WritableStream.

Calling the
ReadableStream.getReader ()

method, you finally get a
ReadableStreamDefaultReader, that later you can read ()

data from until the sequence ends, that is, until it returns an object of the form
{value: undefined, done: true}.

Consequently, calling the
WritableStream.getWriter ()

method, you finally get a
WritableStreamDefaultWriter, that later you can write ()

data a.

  const wss = new WebSocketStream(WSS_URL);
const {readable, writable} = await wss.connection;
const reader = readable.getReader();
const writer = writable.getWriter();

while (true) {
const {value, done} = await reader.read();
if (done) {
break;
}
const result = await process(value);
await writer.write(result);
}

Back pressure

What about the promised back pressure function? As I wrote earlier, you get it "free", no extra steps required. Yes process () takes longer, the next message will only be consumed once the pipeline is ready. Also, the WritableStreamDefaultWriter.write () The step will only continue if it is safe to do so.

Advanced examples

The second argument to WebSocketStream is a bag of options to allow future extension. Currently the only option is protocols, which behaves the same as the
second argument to the WebSocket constructor:

const chatWSS = new WebSocketStream(CHAT_URL, {protocols: ['chat', 'chatv2']});
const {protocol} = await chatWSS.connection;

The selected protocol as well as potential extensions are part of the dictionary available through the WebSocketStream.connection promise. All information about the live connection is provided by this promise, as it is not relevant if the connection fails.

const {readable, writable, protocol, extensions} = await chatWSS.connection;

Information about the closed WebSocketStream connection

The information that was available in the
WebSocket.onclose and
WebSocket.onerror events in the WebSocket API is now available through the WebSocketStream.closed promise. The promise is rejected in case of an unclean close, otherwise it is resolved with the code and reason sent by the server.

All possible status codes and their meaning are explained in the
list of CloseEvent status codes.

const {code, reason} = await chatWSS.closed;

Close a WebSocketStream connection

A WebSocketStream can be closed with a
AbortController. Therefore, spend a AbortSignal

to the WebSocketStream builder.

const controller = new AbortController();
const wss = new WebSocketStream(Url, {signal: controller.signal});
setTimeout(() => controller.abort(), 1000);

Alternatively, you can also use the WebSocketStream.close () method, but its main purpose is to allow you to specify the
code
and reason why it is sent to the server.

wss.close({code: 4000, reason: 'Game over'});

Interoperabilidad y progressive improvementLa mejora progresiva es una estrategia de diseño web diseñada para permitir que el contenido básico de una web se muestre independientemente de la tecnología del navegador, la conexión a Internet o el tipo de dispositivo final. A la vez, no obstante, la web en cuestión además debería ofrecer una versión completa diseñada para satisfacer los mejores requerimientos técnicos posibles. Un resultado de esta estrategia es un diseño responsive. Una plus

Currently, Chrome is the only browser that implements the WebSocketStream API. For interoperability with the classic WebSocket API, it is not possible to apply back pressure to received messages. It is possible to apply back pressure to sent messages, but it does involve polling the
WebSocket.bufferedAmount

property, which is inefficient and not ergonomic.

Feature detection

To check if the WebSocketStream API is supported, use:

if ('WebSocketStream' in window) {
}

Manifestation

In the browsersConcepto de Navegadores¿Qué son los Navegadores?Los Navegadores son herramientas informáticas que utilizamos para, normalmente, navegar por Internet y visitar cualquier página web, al mismo tiempo de para hacer otras tareas como ver documentos, observar vídeos o reproducir contenido multimedia de cualquier tipo. Son un tipo de software sencillamente usual y bastante utilizado hoy en día.Insistimos en que es algo que se utiliza con mucha frecuencia, puesto que moverse por Internet plus compatibles, puede ver la API de WebSocketStream en acción en el iframeConcepto de Iframe¿Qué es un Iframe?Un Iframe es un documento de una web que se inserta en otra página web. Consiste en un elemento que puede albergar cualquier tipo de contenido en su interior y que ayuda, entre otras cosas, a ampliar el mensaje que se ofrece con fuentes externas o con material complementario que pueda resultar de interés para el usuario que entra.Este elemento web se usa especialmente al plus incrustado, o directly in Glitch.

Feedback

The Chrome team wants to hear about your experiences with the WebSocketStream API.

Tell us about the API design

Is there something in the API that is not working as you expected? Or are you missing any methods or properties you need to implement your idea? Have a question or comment about the security model? File a spec issue in the corresponding GitHub repositoryor add your thoughts to an existing problem.

Report a problem with the deployment

Found a bug with the Chrome implementation? Or is the implementation different from the specification? File a bug in new.crbug.com. Be sure to include as much detail as you can, simple instructions to reproduce, and enter Blink> Network> WebSockets at Components box.
Failure works great for quick and easy replay case sharing.

Show API support

Thinking of using the WebSocketStream API? Your public support helps the Chrome team prioritize features and shows other browser vendors how important it is to support them.

Send a tweetUn tweet es un mensaje corto que puede ser enviado por medio de del servicio de microblogging Twitter. ¿Qué es un tweet? Un tweet es un mensaje de estado en Twitter que puede tener hasta 280 caracteres. Los tweets solían limitarse a 140 caracteres, pero esta cantidad se incrementó a 280 en septiembre de 2017. Debido al espacio limitado para el texto, las URLs para uso en tweets son normalmente plus to @Cromodev with the #WebSocketStream hashtagConcepto de Hashtag¿Qué es un Hashtag?Un Hashtag es una almohadilla (#), o más bien, una palabra que se adjunta a esta para crear una etiqueta en determinadas redes sociales. Utilizada en un comienzo por Twitter, poco a poco se ha ido extendiendo por demás rincones de la red de redes, hasta el punto de ser uno de los componentes esenciales al momento de transmitir información, agrupar contenidos o catalogar tendencias.Facebook, plus and let us know where and how you are using it.

Helpful Links

Thanks

The WebSocketStream API was implemented by Adam Rice and
Yutaka hirano. Hero image of Daan mooij in
Unsplash.