|
| 1 | +## What is PackMe |
| 2 | +PackMe is a lightweight library for packing your data into binary buffer (presumably in order to be sent over tcp connection) and unpacking it back to class objects described in a simple way via JSON manifest files. |
| 3 | + |
| 4 | +## It is Fast |
| 5 | +Spoiler alert! ~500k pack/unpack cycles per second for data of average size and complexity. Of course it depends on system configuration :) |
| 6 | + |
| 7 | +Since PackMe generates .dart classes, there is no need for any resource demanding serialization/deserialization process. No intermediate steps involved, every class has it's own efficient methods to quickly put all data to Uint8List buffer and extract it. Comparing to popular solutions it's performance is similar to FlatBuffers and greatly outperforms Proto Buffers. |
| 8 | + |
| 9 | +## It is Simple |
| 10 | +No special file formats (like for FlatBuffers or Proto Buffers manifest files), just use JSON. Objects, types and messages declarations are very simple and intuitive. |
| 11 | + |
| 12 | +## Usage |
| 13 | +The best way of using it for client-server applications is by using ConnectMe package which provides all necessary stuff like adding message listeners, calling asynchronous queries etc. But you can use it separately as well. |
| 14 | + |
| 15 | +Here's a simple manifest.json file (located in packme directory) for some hypothetical client-server application: |
| 16 | +```json |
| 17 | +{ |
| 18 | + "get_user": [ |
| 19 | + { |
| 20 | + "id": "string" |
| 21 | + }, |
| 22 | + { |
| 23 | + "first_name": "string", |
| 24 | + "last_name": "string", |
| 25 | + "age": "uint8" |
| 26 | + } |
| 27 | + ] |
| 28 | +} |
| 29 | +``` |
| 30 | +Generate dart files: |
| 31 | +```bash |
| 32 | +node compile.js packme generated |
| 33 | +``` |
| 34 | +Using on client side: |
| 35 | +```dart |
| 36 | +import 'generated/manifest.generated.js'; |
| 37 | +import 'package:packme/packme.dart'; |
| 38 | +
|
| 39 | +... |
| 40 | +
|
| 41 | +PackMe packMe = PackMe(); |
| 42 | +packMe.register(manifestMessageFactory); // Required by PackMe to create class instances while unpacking messages |
| 43 | +
|
| 44 | +GetUserRequest request = GetUserRequest(id: 'a7db84cc2ef5012a6498bc64334ffa7d'); |
| 45 | +socket.send(packMe.pack(request)); // Some socket implementation |
| 46 | +
|
| 47 | +socket.listen((Uint8List data) { |
| 48 | + final PackMeMessage? message = packMe.unpack(data); |
| 49 | + if (message is GetUserResponse) { |
| 50 | + print('He is awesome: ${message.firstName} ${message.lastName}, ${message.age} y.o.'); |
| 51 | + } |
| 52 | +}); |
| 53 | +``` |
| 54 | +Using on server side: |
| 55 | +```dart |
| 56 | +import 'generated/manifest.generated.dart'; |
| 57 | +import 'package:packme/packme.dart'; |
| 58 | +
|
| 59 | +... |
| 60 | +
|
| 61 | +PackMe packMe = PackMe(); |
| 62 | +packMe.register(manifestMessageFactory); // Required by PackMe to create class instances while unpacking messages |
| 63 | +
|
| 64 | +server.listen((Uint8List data, SomeSocket socket) { // Some server implementation |
| 65 | + final PackMeMessage? message = packMe.unpack(data); |
| 66 | + if (message is GetUserRequest) { |
| 67 | + GetUserResponse response = GetUserResponse( |
| 68 | + firstName: 'Peter', |
| 69 | + lastName: 'Hollens', |
| 70 | + age: '39' |
| 71 | + ); |
| 72 | + socket.send(packMe.pack(response)); |
| 73 | + } |
| 74 | +}); |
| 75 | +``` |
| 76 | + |
| 77 | +## Supported platforms |
| 78 | +Now it's only for Dart. Will it be cross platform? Well it depends... If developers will find this package useful then it will be implemented for JavaScript and C++ I guess. |
0 commit comments