-
-
Notifications
You must be signed in to change notification settings - Fork 52
Protobufs
Let's not waste time here, if you've already read the Encoding section, we can start right away.
Install the protocol-buffers generator by downloading a github release of it and add it to your $PATH env variable, you can find the releases at this github page.
Details about protocol-buffers for Go can be found at evelopers.google.com/protocol-buffers/docs/gotutorial.
Let's start by defining what structure looks like.
Create a new file on your package named user_message.proto
and fill it.
syntax="proto3";
package main;
message UserMessage {
string Username =1;
string Text = 2;
}
Protobufs requires code generation.
Start a session terminal and execute:
$ protoc --go_out=. user_message.proto
This will create a user_message.pb.go
which extends the data structure in protobuf form for Go.
- https://github.com/protocolbuffers/protobuf/tree/master/js#commonjs-imports
-
https://github.com/protobufjs/protobuf.js#nodejs (with browserify)
- We use that in _examples/protobuf/browser repository example.
- https://github.com/protobufjs/protobuf.js#browsers (alternative)
userMsg := &UserMessage{
Username: "my username",
Text: "text from terminal",
}
// send data to the "chat" event.
body, err := proto.Marshal(userMsg)
if err != nil {
// [handle err...]
}
c.EmitBinary("chat", body)
// assuming that we are on the other side's "chat" event's callback,
// here is how you can read protobuf data.
var userMsg UserMessage
if err := proto.Unmarshal(msg.Body, &userMsg); err != nil {
return err
}
// userMsg.Username == "my username"
// userMsg.Text == "text from terminal"
Read the complete source code by navigating to the repository's _examples/protobuf directory.
Home | About | Project | Getting Started | Technical Docs | Copyright © 2019-2023 Gerasimos Maropoulos. Documentation terms of use.