Handling binary blob XMLHttpRequest call

86 views
Skip to first unread message

JamieOliver

unread,
Aug 10, 2014, 1:12:10 AM8/10/14
to mongoos...@googlegroups.com
Hi, 

I have Mongoose server built in to a C++ application. The C++ application holds arrays of floating point numbers, and I would like to request this data from a Javascript page (also served by my application) using XMLHttpRequest, as a binary blob. How can I use Mongoose to handle and service this request?

Thanks,
Jamie

Sergey Lyubka

unread,
Aug 10, 2014, 4:54:35 AM8/10/14
to mongoose-users
I would rather return a JSON object that holds an array of Numbers. Reason: independence from the platform-specific representation of floats.

However if you'd like to send a binary blob instead, do:

case MG_REQUEST:
   if (strcmp(conn->uri, "/get_floats") == 0) {
     mg_send_header(conn, "Content-Type", "application/octet-stream");
     mg_send_data(conn, float_array_pointer, size_of_float_array);
     return MG_TRUE;
   }
   ...


--
You received this message because you are subscribed to the Google Groups "mongoose-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongoose-user...@googlegroups.com.
To post to this group, send email to mongoos...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongoose-users.
For more options, visit https://groups.google.com/d/optout.

Jamie Vicary

unread,
Aug 11, 2014, 8:19:04 AM8/11/14
to mongoos...@googlegroups.com
Hi, thanks for your reply. JSON is a plain-text format, right? I would
worry about the increased size of the data packet.

Is there a platform-safe way to transmit binary data?

Cheers
Jamie
> You received this message because you are subscribed to a topic in the
> Google Groups "mongoose-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/mongoose-users/gKs85nB4b-M/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

Sergey Lyubka

unread,
Aug 11, 2014, 8:51:55 AM8/11/14
to mongoose-users
On Mon, Aug 11, 2014 at 1:18 PM, Jamie Vicary <jamie...@gmail.com> wrote:
Hi, thanks for your reply. JSON is a plain-text format, right? I would
worry about the increased size of the data packet.

Is there a platform-safe way to transmit binary data?

There are plenty.
All of them transform binary data to the platform independent data using some sort of encoding.
JSON is great cause it has relatively small overhead, and is fully human readable.

If you're 100% sure that sending and receiving sides are on of the same platform, sending binary blob is an option too -- although it is still a risky play (a software could be built with different compiler options, etc).

If you're talking about doubles (which are 8 bytes on common architectures), JSON-encoding might be actually even tighter then the original array. Consider array of zeroes: JSON would be [0,0,0,...],
each number taking 2 bytes instead of 8 bytes.

jeff shanab

unread,
Aug 11, 2014, 9:00:16 AM8/11/14
to mongoos...@googlegroups.com
I use base64 encoding within json for arbitray binary data. Images, encrypted data, C++ POD objects (ok that one is rare)


--

Sergey Lyubka

unread,
Aug 12, 2014, 4:18:42 AM8/12/14
to mongoose-users
Yes, base64-encoding binary blobs and wrapping resulting text into JSON is a common practice. Many popular RESTful services do that, for example GitHub API encodes file text that way.

Base64 encoding has exactly 4/3 overhead, i.e. output text is 33% larger then the input data. That determinism is a nice property of base64.

Using base64 still means that binary blob is transferred. Representation of floats in binary form is standartized by IEEE (2 formats for single and double precision).

So two basic choices are:

   1. Send a binary blob (base64-encoded or not) of floats. Also send a hint (in a HTTP header for example), which float format is used (e.g. "floats are IEEE 754 doubles, with no holes between them", something like "double arr[N]" in C language would produce). The receiving side would have all information to unpack the blob properly. Pro: easy to do for C/C++, no need to transform if both sides use the same standard for floats. Cons: extra care about exact binary format.

   2. Transform floats into text, like to JSON or some other format. Pro: independence from the binary format, easy to debug by a human. Cons: both sides need to transform, transfer size could be inflated.


Jamie Vicary

unread,
Aug 12, 2014, 4:25:59 AM8/12/14
to mongoos...@googlegroups.com
Thanks, I think I will choose the JSON option. Inflated size can be
tackled with LZ4 compression.
> You received this message because you are subscribed to a topic in the
> Google Groups "mongoose-users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/mongoose-users/gKs85nB4b-M/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to

jeff shanab

unread,
Aug 12, 2014, 6:48:32 AM8/12/14
to mongoos...@googlegroups.com
Not sure where the data is sourceing and sinking in the customers example but Google Protocol Buffers has a binary transfer with all the conversion built in.
Reply all
Reply to author
Forward
0 new messages