2016-12-01 16:17:55 -02:00
|
|
|
# goevents [](https://travis-ci.org/eventials/goevents) [](http://godoc.org/github.com/eventials/goevents) [](https://goreportcard.com/report/github.com/eventials/goevents)
|
|
|
|
|
|
|
|
Go messaging library
|
|
|
|
|
|
|
|
## About
|
|
|
|
|
|
|
|
`goevents` allows to dispatch events between applications.
|
|
|
|
|
|
|
|
An application produces events based on actions.
|
|
|
|
Another application consume these events and maybe create new events.
|
|
|
|
|
2017-06-15 12:07:50 -03:00
|
|
|
*Scenario:* If an application produces an event "payment.received", another application may want to delivery the product to the buyer.
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
## Supported Transport
|
|
|
|
|
|
|
|
- AMQP
|
|
|
|
|
|
|
|
## How to use
|
|
|
|
|
|
|
|
**The consumer**
|
|
|
|
|
|
|
|
```go
|
2016-12-28 09:20:29 -02:00
|
|
|
conn, err := NewConnection("amqp://guest:guest@127.0.0.1:5672/")
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
defer conn.Close()
|
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
c, err := NewConsumer(conn, false, "events-exchange", "events-queue")
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
defer c.Close()
|
|
|
|
|
2016-12-01 16:58:20 -02:00
|
|
|
c.Subscribe("object.*", func(body []byte) bool {
|
2016-12-01 16:17:55 -02:00
|
|
|
fmt.Println(body)
|
|
|
|
return true
|
|
|
|
})
|
2016-12-28 09:20:29 -02:00
|
|
|
|
|
|
|
go c.Consume()
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
select{}
|
2016-12-01 16:17:55 -02:00
|
|
|
```
|
|
|
|
|
|
|
|
**The producer**
|
|
|
|
|
|
|
|
```go
|
2016-12-28 09:20:29 -02:00
|
|
|
conn, err := NewConnection("amqp://guest:guest@127.0.0.1:5672/")
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
defer conn.Close()
|
|
|
|
|
2016-12-28 09:20:29 -02:00
|
|
|
p, err := NewProducer(conn, "events-exchange", "events-queue")
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 16:52:17 -03:00
|
|
|
defer p.Close()
|
|
|
|
|
2016-12-01 16:58:20 -02:00
|
|
|
err = p.Publish("object.my_action", []byte("message"))
|
2016-12-01 16:17:55 -02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
```
|
2019-03-12 16:52:17 -03:00
|
|
|
|
|
|
|
## Important
|
|
|
|
|
|
|
|
When using `producer`, always close all your producers (things who call the producer.Publish) before closing the producer itself (producer.Close).
|
|
|
|
In this way, you have more garanties that your messages is delivered to RabbitMQ.
|