📜 ⬆️ ⬇️

MessagePack analog JSON, but faster and less

MessagePack is an efficient data serializer in a binary representation.



Using this library, you can exchange messages between your system components in the same way you do it using the JSON format. Library interpretation exists for 44 programming languages ​​(!)

Some simple visual examples in PHP:
')
php -r 'echo strlen(json_encode(array("Hello World!")));' // 16 php -r 'echo strlen(msgpack_pack(array("Hello World!")));' // 14 

 php -r 'echo strlen(msgpack_pack(array("Hello World!", "Hello World!")));' // 27 php -r 'echo strlen(json_encode(array("Hello World!", "Hellow World!")));' // 32 

 php -r 'echo strlen(json_encode(array("Hello World!" => "Hello World!")));' // 31 php -r 'echo strlen(msgpack_pack(array("Hello World!" => "Hello World!")));' // 27 

These synthetic examples show that MessagePack packs data 15-20% more efficiently than JSON. In the loaded project, the exchange of messages between the components of the system totals 100 thousand per day or more. Saving 15-20% of the traffic you can serve more messages on one node for the same period of time.

The library is fully compatible with JSON, but it’s still worth knowing some limitations:

You can find out more about the data packaging specification at github.com/msgpack/msgpack/blob/master/spec.md

Installing and configuring the library is very trivial and the developers take care not to create difficulties at this stage. So, if you use PHP, it all comes down to the familiar construction:

 pecl install msgpack 
Example for Python:
 pip install u-msgpack-python // easy_install u-msgpack-python 
For Ruby:
 gem install msgpack 

The library is over 3 years old.

The latest actual version of the extension for PHP currently is 0.5.5.
It noted that in addition to the standard functions msgpack_pack and msgpack_unpack (which are aliases to msgpack_serialize and msgpack_unserialize) there are two more undocument classes of MessagePack and MessagePackUnpacker. They have the following interfaces:

Messagepack
 MessagePack::__construct(boolean $opt = null); // $opt -    php.ini msgpack.php_only   MessagePack::setOption(integer $option, boolean $value); //    MSGPACK_CLASS_OPT_PHPONLY MessagePack::pack(mixed $value); //      MessagePack::unpack(string $str, $object = null); //  .     , , ,           . MessagePack::unpacker(); //    MessagePackUnpacker.  ,            . 

MessagePackUnpacker
 MessagePackUnpacker::__construct(boolean $opt = null); // $opt -    php.ini msgpack.php_only   MessagePackUnpacker::__destruct(); MessagePackUnpacker::setOption(integer $option, boolean $value); //    MSGPACK_CLASS_OPT_PHPONLY MessagePackUnpacker::feed(string $str); //      .  true  false. MessagePackUnpacker::execute(string $str = null, integer &$offset = null); //  .  true  false. MessagePackUnpacker::data(mixed $object = null); //  ,        execute  false.  ,          $object -       reset.  ,         $object    MessagePack::unpack    reset. MessagePackUnpacker::reset(); //   ; 

The official website of the developers msgpack.org
PS: I do not belong to the library developers. I use it in work 4 months.

Source: https://habr.com/ru/post/251177/


All Articles