Protobuf.js: Request: a hello world sample

Created on 30 Apr 2018  路  3Comments  路  Source: protobufjs/protobuf.js

Please provide a small, self-contained hello world sample jsfiddle with (<script src="https://cdn.rawgit.com/dcodeIO/protobuf.js/master/dist/protobuf.min.js" />) for beginners.

I was trying and failing to assemble one example:

  • hardcoded/inline type definition
  • simple hardcoded message with a string "Hello World!", an integer 123 and a float -123.456
  • encode and convert to byte array
  • decode and display the fields in a div / something html

This way one example will cover how to reference this lib on a simple HTML page (with no other external dependencies), usage of types and encode/decode APIs. If someone then wants to incorporate the code in their project using commonJS/es6/amd etc. they can easily convert it.

Thanks!

Most helpful comment

Thanks! I was hoping to have a very basic browser-based example (no deps on running server or node.js etc.) which creates a message object, serialize it to binary protobuf, then deserialize that binary back to object to assert:
assert.equal(originalObject, deserialize(serialize(originalObject)))

All 3 comments

@kasper3 here are some examples here

Thanks! I was hoping to have a very basic browser-based example (no deps on running server or node.js etc.) which creates a message object, serialize it to binary protobuf, then deserialize that binary back to object to assert:
assert.equal(originalObject, deserialize(serialize(originalObject)))

I was looking to represent .proto in .json and use grpc in node and was really happy to find this repo. :thumbsup:

However, I am looking to figure out a basic example of grpc using protobufjs.

I did manage to convert .proto to .json using pbjs (see below).

Can any of the moderators provide the code for greeter_server.js and greeter_client.js using protobufjs please. 馃檹 馃檹

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
}
{
  "nested": {
    "helloworld": {
      "nested": {
        "Greeter": {
          "methods": {
            "SayHello": {
              "requestType": "HelloRequest",
              "responseType": "HelloReply"
            }
          }
        },
        "HelloRequest": {
          "fields": {
            "name": {
              "type": "string",
              "id": 1
            }
          }
        },
        "HelloReply": {
          "fields": {
            "message": {
              "type": "string",
              "id": 1
            }
          }
        }
      }
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings