github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/docs/v2/library/go-pubsub.md (about) 1 --- 2 title: PubSub 3 keywords: go-micro, framework, pubsub 4 tags: [go-micro, framework, pubsub] 5 sidebar: home_sidebar 6 permalink: /go-pubsub 7 summary: Go Micro builds in PubSub for event driven microservices 8 --- 9 10 # Overview 11 12 Microservices is an event driven architecture patterna and so Go Micro builds in the concept of asynchronous messaging 13 using a message broker interface. It seamlessly operates on protobuf types for you. Automatically encoding and decoding 14 messages as they are sent and received from the broker. 15 16 By default go-micro includes a point-to-point http broker but this can be swapped out via [go-plugins](https://github.com/micro/go-plugins). 17 18 ### Publish Message 19 20 Create a new publisher with a `topic` name and service client 21 22 ```go 23 p := micro.NewEvent("events", service.Client()) 24 ``` 25 26 Publish a proto message 27 28 ```go 29 p.Publish(context.TODO(), &proto.Event{Name: "event"}) 30 ``` 31 32 ### Subscribe 33 34 Create a message handler. It's signature should be `func(context.Context, v interface{}) error`. 35 36 ```go 37 func ProcessEvent(ctx context.Context, event *proto.Event) error { 38 fmt.Printf("Got event %+v\n", event) 39 return nil 40 } 41 ``` 42 43 Register the message handler with a `topic` 44 45 ```go 46 micro.RegisterSubscriber("events", ProcessEvent) 47 ``` 48 49 See [examples/pubsub](https://github.com/micro/examples/tree/master/pubsub) for a complete example. 50