myHotTake

Tag: binary data in JavaScript

  • How Does JavaScript Handle WebSocket Binary Data?

    If you enjoyed this story, feel free to give it a thumbs up or share it with a friend who loves tech tales!


    Once upon a time, I found myself in the midst of an art gallery, where paintings and sculptures were being transported to and fro. In this gallery, I realized there were two types of art: paintings full of color and intricate sculptures carved from stone. I was amazed at how effortlessly the gallery handled both, and it reminded me of how WebSockets manage data.

    In this gallery, paintings represented text data—clear, colorful, and easy to interpret at first glance. The paintings were displayed in frames, much like text data in WebSockets is encapsulated in frames for easy transport and viewing.

    On the other hand, sculptures symbolized binary data—complex, heavy, and requiring a thoughtful approach to appreciate. The gallery had special crates for sculptures, just as WebSockets have binary frames to transport binary data. These crates ensured that sculptures, much like binary data, were protected and delivered in their true form without losing any detail in transit.

    As I walked through the gallery, I watched the curator seamlessly guide both paintings and sculptures to their destinations. This reminded me of how WebSockets can switch between text and binary data effortlessly, ensuring that both types of content reach their intended audience without a hitch. Just as the gallery needed to cater to art lovers of all kinds, WebSockets cater to applications that require both textual and binary data exchanges.

    In this way, I realized that the art of handling diverse data types was much like running an art gallery. Both require careful management and a deep appreciation for the different forms of expression. So, whether it’s paintings or intricate sculptures, text or binary data, the gallery—and WebSockets—handle them all with grace and efficiency.


    First, the curator showed me how they handle paintings, or text data, using WebSockets in JavaScript. They opened a small window to the world of code:

    const socket = new WebSocket('ws://example.com/socket');
    
    socket.onopen = function(event) {
      console.log('Connection established!');
      socket.send('Hello, Server!'); // Sending text data
    };
    
    socket.onmessage = function(event) {
      console.log('Message from server ', event.data); // Receiving text data
    };

    I watched as the curator sent and received messages, just like sending and receiving paintings. The paintings traveled smoothly, with each brushstroke preserved, through this WebSocket connection.

    Next, the curator turned their attention to the sculptures, or binary data. They explained how JavaScript handles these intricate pieces:

    socket.binaryType = 'arraybuffer'; // Setting binary data type
    
    socket.onopen = function(event) {
      console.log('Connection established!');
    
      const binaryData = new Uint8Array([1, 2, 3, 4]); // Creating binary data
      socket.send(binaryData.buffer); // Sending binary data
    };
    
    socket.onmessage = function(event) {
      const receivedData = new Uint8Array(event.data);
      console.log('Binary message from server ', receivedData); // Receiving binary data
    };

    In this part of the gallery, I saw how the sculptures were carefully packed and unpacked, much like binary data in JavaScript. The use of ArrayBuffer and Uint8Array ensured that every chisel mark and curve was preserved, allowing the sculptures to be displayed in all their glory.

    Key Takeaways:

    1. WebSocket Versatility: WebSocket in JavaScript can handle both text and binary data, similar to an art gallery managing different forms of art.
    2. Data Framing: Text data is straightforward, while binary data requires proper framing using ArrayBuffer and Uint8Array to ensure integrity.
    3. Dynamic Handling: JavaScript allows seamless switching between data types, just as a curator artfully manages diverse artworks.