4

I am trying to send a message over MQTT making use of the google protocol buffer in javascript (ProtoBuf.js)

I was able to encode the message using the following code:

var ProtoBuf = dcodeIO.ProtoBuf;
var builder = ProtoBuf.loadProtoFile("./complex.proto"),
Game = builder.build("Game"),
Car = Game.Cars.Car;
var car = new Car({
"model" : "Rusty",
"vendor" : {
            "name" : "Iron Inc.",
           "address" : {
                "country" : "USa"
             }
          },
    "speed" : "FAST"
 });
 var buffer = car.encode();
console.log(buffer);
var messagegpb = buffer.toBuffer();
console.log(messagegpb ); //This prints "ArrayBuffer { byteLength: 29 }"

Now for decoding when I tried the following, it just doesn't do anything. I see no logs in the browser as well.

var dec = builder.build("Game"); //nothing after this line gets executed
var msg = dec.decode(messagegpb);
console.log(msg);

This is the link of the .proto file I am using. https://github.com/dcodeIO/ProtoBuf.js/blob/master/tests/complex.proto

Could someone point me where I am going wrong?

Thanks a ton

1 Answer 1

2

Presumably these lines:

var dec = builder.build("Game");
var msg = dec.decode(messagegpb);

Need to be:

var Game = builder.build("Game");
var msg = Game.Cars.Car.decode(messagegpb);

That is, you need to specify what type you're decoding.

Probably your attempt to call dec.decode was throwing an exception saying the decode method didn't exist. You should have been able to see these exceptions on the error console, or caught them with try/catch.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much Kenton. Actually I figured that problem. But now facing a bigger problem. I am able to decode this message but when i receive a message from a websocket which was encoded by a java client, the decoding doesnt work. Any idea about that? Thanks a lot.
Basically this is what I am facing now. stackoverflow.com/questions/30385865/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.