github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/event/README.md (about) 1 # Event 2 3 This is an example of using the micro API as an event gateway with the event handler 4 5 A http request is formatted as an [event](https://github.com/micro/go-api/blob/master/proto/api.proto#L28L39) and published on the go-micro message broker. 6 7 ## Contents 8 9 - srv - A service which subscribes to events 10 11 ## Usage 12 13 Run the micro api with the event handler set and with a namespace which used as part of the topic name 14 15 ``` 16 micro api --handler=event --namespace=go.micro.evt 17 ``` 18 19 Run the service 20 21 ``` 22 go run srv/main.go 23 ``` 24 25 ### Event format 26 27 On the receiving end the message will be formatted like so: 28 29 ``` 30 // A HTTP event as RPC 31 message Event { 32 // e.g login 33 string name = 1; 34 // uuid 35 string id = 2; 36 // unix timestamp of event 37 int64 timestamp = 3; 38 // event headers 39 map<string, Pair> header = 4; 40 // the event data 41 string data = 5; 42 } 43 ``` 44 45 ### Publish Event 46 47 Publishing an event is as simple as making a http post request 48 49 ``` 50 curl -d '{"name": "john"}' http://localhost:8080/user/login 51 ``` 52 53 This request will be published to the topic `go.micro.evt.user` with event name `login` 54 55 ### Receiving Event 56 57 A subscriber should be registered with the service for topic `go.micro.evt.user` 58 59 The subscriber should take the proto.Event type. See srv/main.go for the code. 60 61 The event received will look like the following 62 63 ``` 64 { 65 name: "user.login", 66 id: "go.micro.evt.user-user.login-693116e7-f20c-11e7-96c7-f40f242f6897", 67 timestamp:1515152077, 68 header: {...}, 69 data: {"name": "john"} 70 } 71 ```