An experimental pub/sub client and server project.

update readme docs and also made example better (#11)

authored by willdot.net and committed by GitHub b7137e3a 8e8e0e1c

Changed files
+32 -7
cmd
server
example
+15 -1
README.md
··· 20 20 21 21 When sending a message representing an action (subscribe, publish etc) then a uint16 binary message is sent. 22 22 23 - When sending any other data, the length of the data is to be sent first using a binary uint32 and then the actual data sent afterwards. 23 + When sending any other data, the length of the data is to be sent first using a binary uint32 and then the actual data sent afterwards. 24 + 25 + ## Running the server 26 + 27 + There is a server that can be run using `docker-compose up message-server`. This will start a server running listening on port 3000. 28 + 29 + ## Example clients 30 + There is an example application that implements the subscriber and publishers in the `example` directory. 31 + 32 + Run `go build .` to build the file. 33 + 34 + When running the example there are the following flags: 35 + 36 + `publish` : settings this to true will allow messages to be sent every 500ms as well as consuming 37 + `consume-from` : this allows you to specify what message to start from. If you don't set this or set it to be -1, you will start consuming from the next sent message.
+7
docker-compose.yaml
··· 1 + version: "3.7" 2 + services: 3 + message-server: 4 + build: 5 + context: . 6 + dockerfile: dockerfile.server 7 + ports: [ "3000:3000" ]
+1 -1
dockerfile.example-server dockerfile.server
··· 3 3 WORKDIR /app 4 4 5 5 COPY go.mod go.sum ./ 6 - COPY example/server/ ./ 6 + COPY cmd/server/ ./ 7 7 RUN go mod download 8 8 9 9 COPY . .
+9 -5
example/main.go
··· 11 11 "github.com/willdot/messagebroker/internal/server" 12 12 ) 13 13 14 - var consumeOnly *bool 14 + var publish *bool 15 15 var consumeFrom *int 16 16 17 + const ( 18 + topic = "topic-a" 19 + ) 20 + 17 21 func main() { 18 - consumeOnly = flag.Bool("consume-only", false, "just consumes (doesn't start server and doesn't publish)") 22 + publish = flag.Bool("publish", false, "will also publish messages every 500ms until client is stopped") 19 23 consumeFrom = flag.Int("consume-from", -1, "index of message to start consuming from. If not set it will consume from the most recent") 20 24 flag.Parse() 21 25 22 - if !*consumeOnly { 26 + if *publish { 23 27 go sendMessages() 24 28 } 25 29 ··· 38 42 startAt = *consumeFrom 39 43 } 40 44 41 - err = sub.SubscribeToTopics([]string{"topic a"}, startAtType, startAt) 45 + err = sub.SubscribeToTopics([]string{topic}, startAtType, startAt) 42 46 if err != nil { 43 47 panic(err) 44 48 } ··· 69 73 i := 0 70 74 for { 71 75 i++ 72 - msg := client.NewMessage("topic a", []byte(fmt.Sprintf("message %d", i))) 76 + msg := client.NewMessage(topic, []byte(fmt.Sprintf("message %d", i))) 73 77 74 78 err = publisher.PublishMessage(msg) 75 79 if err != nil {
example/server/main.go cmd/server/main.go