protobuf-NATS-publish-subscribe/

directory
v0.0.0-...-a2a1f02 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2020 License: MIT

README

protobuf-NATS-publish-subscribe example

Using NATS (publish/subscribe) as a pipe to send protobuf messages. This is 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 have a client publish a message every second to NATS and whoever (server) is subscribed will get the message. This is referred to as one-to-many.

I use the term client/server for lack of a better term. I could of used publisher/subscriber.

In separate windows run,

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

You can run as many servers as you want and you will see they all get the same message.

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)
nc.Publish("foo", msg)
RECEIVE (SUBSCRIBE)

NextMsg will block/wait for the next msg with a timeout of 5 seconds.

sub, err := nc.SubscribeSync("foo")
...
msg, err := sub.NextMsg(time.Duration(5) * time.Second)
UNMARSHAL -> DATA
rcvPerson := &Person{}
err = proto.Unmarshal(msg.Data, rcvPerson)

HIGH-LEVEL ILLUSTRATION

This illustration may help show what we did,

IMAGE - protobuf-NATS-publish-subscribe - IMAGE

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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