Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending binary file through RabbitMQ

Tags:

rabbitmq

I'm spec'ing a design right now that uses RabbitMQ as a message queue. The message are going to have a JSON body, and for one message in particular I'd like to add a small binary file.

What I'd like to know is, should the binary file's data be part of the JSON message, or can it be appended to the message separately?

like image 869
riqitang Avatar asked Feb 27 '14 13:02

riqitang


1 Answers

Since RabbitMQ message payload is just a binary array you should encode your message body with 3 fields:

  1. File size
  2. Binary data of a file
  3. Json

I disagree with a previous answer about embedding a file in json. If you encode file data inside of json you will get wasted space because of json escaping + unnecessary CPU usage because of json encoding/decoding of the file data + you will need to read file data twice (once for json deserialization and once more to copy it where it needs to go)

like image 134
cohadar Avatar answered Sep 21 '22 08:09

cohadar