Can I have flow control on my websockets?

前端 未结 3 1987
庸人自扰
庸人自扰 2021-02-03 10:22

I am using websockets to transfer video-y images from a server, written in Go, to a client which is an HTML page. My experience shared below is with Chrome.

I receive im

相关标签:
3条回答
  • 2021-02-03 10:57

    I doubt what you are asking for is possible. There is no interface for that functionality in the WebSocket API spec. What the spec does outline, however, is a requirement that the underlying socket connection be managed in the background outside of the script that is using the WebSocket, so that the script is not blocked by WebSocket actions. When the socket receives inbound data, it wraps the data inside of a message and queues it for the WebSocket script to process. There is nothing to block the socket from reading more data while messages remain in the queue waiting for the script to process them.

    The only real flow control you can implement in a WebSocket is an explicit one. When a message arrives, send back a message to acknowledge it. Make the server wait to receive that ack before sending its next message.

    0 讨论(0)
  • 2021-02-03 11:11

    It’s now possible to have streams within WebSocket. Chrome 78 will ship with a new WebSocketStream API, which supports backpressure.

    Here’s a quote from Chrome Platform Status:

    The WebSocket API provides a JavaScript interface to the RFC6455 WebSocket protocol. While it has served well, it is awkward from an ergonomics perspective and is missing the important feature of backpressure. The intent of the WebSocketStream API is to resolve these deficiencies by integrating streams with the WebSocket API.

    Currently applying backpressure to received messages is not possible with the WebSocket API. When messages arrive faster than the page can handle them, the render process will either fill up memory buffering those messages, become unresponsive due to 100% CPU usage, or both.

    Applying backpressure to sent messages is possible but involves polling the bufferedAmount property which is inefficient and unergonomic.

    Unfortunately this is a Chrome-only API and there is no web standard at time of writing.

    For further info see:

    • Issue 983030: Implement WebSocketStream
    0 讨论(0)
  • 2021-02-03 11:16

    You can do flow-control on WebSocket connections (based on TCP backpressure adaption). Here are two links to get you started:

    • Flow Control with the Producer/Consumer Pattern
    • Segmented Audio over WebSocket

    Disclosure: I am original author of Autobahn and work for Tavendo.

    0 讨论(0)
提交回复
热议问题