GithubHelp home page GithubHelp logo

Support sending Blob about ws HOT 13 OPEN

kettanaito avatar kettanaito commented on June 1, 2024
Support sending Blob

from ws.

Comments (13)

lpinca avatar lpinca commented on June 1, 2024 1

It is not for historic reasons, see nodejs/undici#1795 (comment).

from ws.

lpinca avatar lpinca commented on June 1, 2024 1

See also nodejs/undici#1795 (comment).

from ws.

lpinca avatar lpinca commented on June 1, 2024 1

It is doable but in my opinion it is not worth it. Added complexity for little to no benefit.

from ws.

lpinca avatar lpinca commented on June 1, 2024 1

Strictly following the WHATWG specification is a non-goal for ws.

from ws.

kettanaito avatar kettanaito commented on June 1, 2024

This is an important context, thanks for sharing! So, Blobs being written first is a Node.js thing?

This is also interesting:

In practice, even though it requires explicitly setting binaryType, 97% of messages are received as ArrayBuffers.

Especially given that the default binaryType value for blob. I believe Undici did end up supporting sending Blob from the client (at least, I seem to be able to do that in tests).

from ws.

lpinca avatar lpinca commented on June 1, 2024

So, Blobs being written first is a Node.js thing?

Yes, because there is no official API to get an ArrayBuffer synchronously from a Blob and streams do not support Blobs natively.

I believe Undici did end up supporting sending Blob from the client (at least, I seem to be able to do that in tests).

I think it is affected by the issue I wrote in that comment.

from ws.

kettanaito avatar kettanaito commented on June 1, 2024

Got it. Thanks for the great references! Do you consider this proposal to be out of scope? We should close it then.

from ws.

kettanaito avatar kettanaito commented on June 1, 2024

What about supporting this only on the surface level? Accepting the Blob but converting it internally to nodebuffer and proceeding as ws does now? If that makes any sense, of course (would love to learn if it doesn't).

My main use case is showcasing ws for testing, and it's not great to omit sending/receiving Blob data.

from ws.

lpinca avatar lpinca commented on June 1, 2024

Accepting the Blob but converting it internally to nodebuffer and proceeding as ws does now?

It is not trivial because again, converting a Blob to an ArrayBuffer/Buffer synchronously is not supported by Node.js. We could pause the Sender like we do when compressing data but I'm not very happy with it. Also what about support for Node.js versions where Blob is not supported?

It is a lot easier to convert the Blob before calling websocket.send().

websocket.send(await blob.arrayBuffer());

from ws.

kettanaito avatar kettanaito commented on June 1, 2024

It is not trivial because again, converting a Blob to an ArrayBuffer/Buffer synchronously is not supported by Node.js.

I keep missing this bit. Got it.

Also what about support for Node.js versions where Blob is not supported?

Blob is supported since Node.js 18, which by itself reaches the maintenance mode in a few months. Does ws support EOL versions of Node.js (16 and older)? If anything, I see this as a motivation to drop those old versions but you would know far better than me here.

from ws.

lpinca avatar lpinca commented on June 1, 2024

Yes, ws still supports Node.js 10.

from ws.

kettanaito avatar kettanaito commented on June 1, 2024

It is a lot easier to convert the Blob before calling websocket.send().

I understand this. What I'm proposing is not a matter of convenience but expectations. As in, valid WebSocket usage as per spec should work. Sending a Blob is a valid usage. Not supporting it is a limitation of ws (as the immediate agent; the issue runs deeper as we've discussed) so it may be a good thing to try to find a solution/compromise here.

I also understand that ws doesn't promise full WHATWG compliance as it's a server library and the spec has nothing about servers, afaik. I'm coming purely from the client usage. If a WebSocket client, like Undici, sends a Blob, it's okay if ws receives and handles it as something else. The issue is that the client will never receive a Blob, even if its binaryType = 'blob' (I will double-check on this but I recall that being the outcome of my testing).

from ws.

lpinca avatar lpinca commented on June 1, 2024

The issue is that the client will never receive a Blob, even if its binaryType = 'blob' (I will double-check on this but I recall that being the outcome of my testing)

That is correct for ws. That binary type is not supported. Receiving a Blob is easier as it can be created synchronously from an ArrayBuffer but if we allow receiving it we should also allow sending it.

from ws.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.