go-micro.dev/v5@v5.12.0/broker/broker.go (about) 1 // Package broker is an interface used for asynchronous messaging 2 package broker 3 4 // Broker is an interface used for asynchronous messaging. 5 type Broker interface { 6 Init(...Option) error 7 Options() Options 8 Address() string 9 Connect() error 10 Disconnect() error 11 Publish(topic string, m *Message, opts ...PublishOption) error 12 Subscribe(topic string, h Handler, opts ...SubscribeOption) (Subscriber, error) 13 String() string 14 } 15 16 // Handler is used to process messages via a subscription of a topic. 17 // The handler is passed a publication interface which contains the 18 // message and optional Ack method to acknowledge receipt of the message. 19 type Handler func(Event) error 20 21 // Message is a message send/received from the broker. 22 type Message struct { 23 Header map[string]string 24 Body []byte 25 } 26 27 // Event is given to a subscription handler for processing. 28 type Event interface { 29 Topic() string 30 Message() *Message 31 Ack() error 32 Error() error 33 } 34 35 // Subscriber is a convenience return type for the Subscribe method. 36 type Subscriber interface { 37 Options() SubscribeOptions 38 Topic() string 39 Unsubscribe() error 40 } 41 42 var ( 43 // DefaultBroker is the default Broker. 44 DefaultBroker = NewHttpBroker() 45 ) 46 47 func Init(opts ...Option) error { 48 return DefaultBroker.Init(opts...) 49 } 50 51 func Connect() error { 52 return DefaultBroker.Connect() 53 } 54 55 func Disconnect() error { 56 return DefaultBroker.Disconnect() 57 } 58 59 func Publish(topic string, msg *Message, opts ...PublishOption) error { 60 return DefaultBroker.Publish(topic, msg, opts...) 61 } 62 63 func Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) { 64 return DefaultBroker.Subscribe(topic, handler, opts...) 65 } 66 67 // String returns the name of the Broker. 68 func String() string { 69 return DefaultBroker.String() 70 }