protobuf-NATS-request-reply/

directory
v0.0.0-...-cf25b83 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 22, 2021 License: MIT

README

protobuf-NATS-request-reply example

Using NATS (request/reply) as a pipe to send protobuf messages. This is a model for a subscriber sending a msg with a request and getting a reply back from a subscriber. This is also a model for one-to-many communication.

These are my 6 main example of using protobuf,

Table of contents,

Documentation and reference,

GitHub Webpage

OVERVIEW OF NATS ARCHITECTURES

We have 3 examples using NATS as a pipe. This diagram may help,

IMAGE - NATS-architectures - IMAGE

START YOUR NATS SERVER

Using NATS as a pipe. First, lets start your NATS server,

nats-server -v
nats-server -DV -p 4222 -a 127.0.0.1

Where -DV is both debug and trace log.

GET NATS GO CLIENT LIBRARY

You must have this library to use go,

go get -v -u github.com/nats-io/nats.go/

PROTOCOL .proto BUFFER FILE

Lets use the same protobuf file messages.proto in all four examples,

message Person {
    string name = 1;
    int32 age = 2;
    string email = 3;
    string phone = 4;
    uint32 count = 5;
}

Compile the protocol buffer file to get the wrappers,

protoc --go_out=. messages.proto

Place wrapper file messages.pb.go in both the client and server directories.

RUN

This example will publish a message every second to NATS and whoever is subscribed will get the message. The subscriber will also send a reply back that it got hte message. The first reply is utilized and the system efficiently discards the additional ones.

In separate windows run,

cd client
go run client.go messages.pb.go
cd server
go run server.go messages.pb.go

It you add multiple servers they all get the message and send a reply back, but the publisher only accepts one reply and discards the rest.

FLOW - HOW DOES IT WORK

First you need to connect to the NATS server in go,

nc, err := nats.Connect("nats://127.0.0.1:4222")
defer nc.Close()

Lets look at the entire flow data -> marshal -> snd -> rcv -> unmarshal -> data.

DATA
sndPerson := &Person{
    Name:  "Jeff",
    Age:   20,
    Email: "blah@blah.com",
    Phone: "555-555-5555",
    Count: count,
}
MARSHAL
msg, err := proto.Marshal(sndPerson)
SEND (PUBLISH) & WAIT FOR REPLY
reply, err := nc.Request("foo", msg, 50*time.Millisecond)
myReply := &MyReply{}
err = proto.Unmarshal(reply.Data, MyReply)
RECEIVE (SUBSCRIBE)
// RECEIVE
nc.Subscribe("foo", func(msg *nats.Msg) {

    // UNMARSHAL -> DATA
    <SEE BELOW>
})
UNMARSHAL -> DATA & SEND REPLY
rcvPerson := &Person{}
err = proto.Unmarshal(msg.Data, rcvPerson)

The reply (MARSHALL & SEND),

myReply := &MyReply{}
myReply.Thereply = fmt.Sprintf("This is a response %v, from count %d", uniqueID, rcvPerson.Count)
replymsg, err := proto.Marshal(myReply)
err = nc.Publish(msg.Reply, replymsg)

HIGH-LEVEL ILLUSTRATION

This illustration may help show what we did,

IMAGE - protobuf-NATS-request-reply - IMAGE

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL