Skip to content
Advertisement

How to read BLOB data from a WebSocket which is not an image

I created a WebSocket connection to my webserver to receive some data. However, when I log the event that I receive in the onmessage function, then I cannot see the real content of the data.

When I copy the network connection that my Chrome browser v32 opens as a curl command and run it on my OS console, then everything works fine. So I think that somehow my WebSocket setup must be wrong. The event.data object is an instance of Blob.

Here is my code (actually CoffeeScript, but easy to understand):

JavaScript

The event that I get:

JavaScript

Advertisement

Answer

Ok, I found the solution! I have to read the data that comes as a Blob with a FileReader:

JavaScript

Alternatively, in ES2015:

JavaScript
Advertisement