0

is there a way to identify binary messages from a server by tagging them with some kind of type attribute?

Background: I'm working with node.js and I'm sending binary messages to my client. At the moment these are only pictures sent as blobs. So on clientside I'm testing incoming messages with instance of Blob and then do something with the pics.

But now I additionally want to send other files like .txt over the websocket protocol (for downloads). I feel like this is much simpler than HTTP requests etc. But now I have to make a difference on incoming binary messages if they are images or textfiles or sth. else.

The Chrome Developertools show me that my incoming images (blobs) have a type attribute that is an empty string. As I read this attribute is read only, so I'm looking for a solution to identify my binary messages like I can do it with JSON Objects...

Thanks in advance for every idea :)

Edit:

Here is one example for sending images to the client. I'm using the "ws" module for nodejs.

fs.readFile(path, function (err, data)
{
    if (!err)
    {
        connection.send(data, {binary: true}, function(err)
        {
            if (!err) console.log("Server finished sending image to client.");
            else console.log("Error while sending imageto client.");
        });
    }
    else console.log("Error while loading image");
});

Where could I add some kind of metadata to the binary data? Base64 encoding is no possibility for me.

Fidel90
  • 1,828
  • 6
  • 27
  • 63
  • This would be helpful : http://stackoverflow.com/questions/5766802/send-and-receive-binary-data-over-web-sockets-in-javascript – user568109 Aug 13 '13 at 17:23

1 Answers1

0

If it's only about the format, you might use the content-type header to specify what kind of file type it is: text/plain or image/png or whatever you want to use ...

Of course, you can extend this idea and use a custom-header to transport any information you would like to transfer.

Or, which is the most flexible solution: Send back a real JSON object, embed all the metadata you need, and provide the binary content encoded within the JSON (using any encoding you like and that is compatible to JSON, such as Base64), such as:

{
  "filename": "foo.txt",
  "content-type": "image/png",
  "binary-data": "A5EBC7FFEFDCD8975BC3..."
}

To use your code snippet as base, you might implement it as follows:

fs.readFile(path, function (err, data) {
    if (err) { throw err; }
    connection.send({
        filename: 'foo.txt',
        contentType: 'image/png',
        data: JSON.stringify(data)
    }, { binary: false }, function (err) {
        if (err) { throw err; }
        console.log('Server finished sending image to client.');
    });
});

Please note that I did not test the code, but your final code should look somewhat similar to this one.

Hope this helps.

Golo Roden
  • 140,679
  • 96
  • 298
  • 425
  • Thanks. I added a code snippet to my question above. I dont know where i could implement a kind of tag or metadata. – Fidel90 Aug 13 '13 at 17:40
  • I updated my answer. I think you should now be able to figure out the rest for yourself. Hope this helps. If so, it would be great if you could upvote the answer and mark it as answer. – Golo Roden Aug 13 '13 at 18:06
  • Thanks, but that's not working for me. I need to receive blobs, not strings. Maybe i just should use standard HTTP Up/Download-Routines. Thanks anyway :) – Fidel90 Aug 14 '13 at 08:55
  • You can still use the approach of (mis)using http headers … – Golo Roden Aug 14 '13 at 09:13