How to write a series of Messages into a file and read them back using protobuf (protobuf.js with proto3)?

328 Views Asked by At

I tried the example and was able to get one Message to work:

// awesome.proto
package awesomepackage;
syntax = "proto3";

message AwesomeMessage {
    string awesome_field = 1; // becomes awesomeField
}

A complete solution is in my answer in this question.

Is protobuf able to let us write multiple message types to a file and read them back? (or through the network, one message after another).

And I think the power is, we can "mix and match" the message type? (meaning it can be Message1, and then Message2 twice or three times, and it can be totally dynamic?)

So let's say we have

// awesome.proto
package awesomepackage;
syntax = "proto3";

message AwesomeMessage {
    string awesome_field = 1; // becomes awesomeField
}

message AwesomeNameMessage {
    string awesome_name = 1; // becomes awesomeName
}

I just followed the example in protobufjs's website, and wrote AwesomeMessage to a file, and then create AwesomeNameMessage and concat it to that same file.

Now is it possible to write read.js and read back the messages without knowing what message types are there in the data file?

I got stuck when I write the read.js

  const AwesomeMessage = root.lookupType("awesomepackage.AwesomeMessage");

The thing is, I don't know whether it is AwesomeMessage or AwesomeNameMessage, so how can we proceed? Can protobuf work the way above, or do we need to define all message types (be it 1000 of them), and then enclosure all of them into one Message type, such as GenericMessage, and somehow use oneof to state that the next message can be any of the message type (or making every message optional)? How does it work?

1

There are 1 best solutions below

0
On

If you control the protobuf protocol specification, it may be a good idea to add types holding your messages.

// awesome.proto
package awesomepackage;
syntax = "proto3";

message MessageList {
    repeated AbstractMessage msgs = 1;
}

message AbstractMessage {
    oneof msgtype {
        AwesomeMessage awesomeMessage = 2;
        AwesomeNameMessage awesomeNameMessage = 3;
        ...
    }
}

message AwesomeMessage {
    string awesome_field = 1; // becomes awesomeField
}

message AwesomeNameMessage {
    string awesome_name = 1; // becomes awesomeName
}