ws: `bufferedAmount` property is broken

The bufferedAmount property doesn’t work correctly.

This code:

var WebSocket = require('ws')

var socket = new WebSocket('wss://echo.websocket.org')

socket.onopen = function () {
  socket.send('abc')
  console.log('bufferedAmount after send: ' + socket.bufferedAmount)
}

socket.onmessage = function () {
  console.log('bufferedAmount after onmessage: ' + socket.bufferedAmount)
}

setInterval(function () {
  console.log('bufferedAmount on interval: ' + socket.bufferedAmount)
}, 1000)

Prints:

bufferedAmount after send: 10
bufferedAmount after onmessage: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
bufferedAmount on interval: 1
...

In Chrome and Firefox, on the other hand, this is what gets output:

bufferedAmount after send: 3
bufferedAmount after onmessage: 0
bufferedAmount on interval: 0
bufferedAmount on interval: 0
bufferedAmount on interval: 0
bufferedAmount on interval: 0
bufferedAmount on interval: 0
bufferedAmount on interval: 0
...

Without a functioning bufferedAmount property, it’s not possible to use the code I’ve written to implement backpressure in the browser on the server as well.

cc @mafintosh @maxogden

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Reactions: 3
  • Comments: 27 (11 by maintainers)

Commits related to this issue

Most upvoted comments

If I understand the issue correctly, bufferedAmount was reporting +1 size even on an empty buffer. This was a fixed in node v8.8.0 (issue #15005)

If that was the issue, then yes, bufferedAmount is correctly reporting zero now after the onmessage. Although, I am still at a loss as to why it took 10 bytes (18 as of v10.8.0) to contain 3 ascii characters. I would love to find out if anyone knows.