https://github.com/devimteam/observer
Golang pub/sub observer
https://github.com/devimteam/observer
amqp golang golang-library
Last synced: 5 months ago
JSON representation
Golang pub/sub observer
- Host: GitHub
- URL: https://github.com/devimteam/observer
- Owner: devimteam
- Created: 2017-06-30T14:29:06.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-03-23T15:35:29.000Z (about 8 years ago)
- Last Synced: 2025-08-09T21:37:47.719Z (10 months ago)
- Topics: amqp, golang, golang-library
- Language: Go
- Size: 36.1 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
RMQ Observer For Pub/Sub Event
============================
Subscribe event - Sub(service string, reply interface{}) (OutCh, error)
Publish event - Pub(service string, data interface{}) error
## Examples
## Sub
``` go
package main
import (
"log"
"github.com/streadway/amqp"
"github.com/l-vitaly/observer"
"github.com/l-vitaly/observer/json"
)
type CreateUserEvent struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
conn, err := amqp.Dial("amqp://rmqhost/")
if err != nil {
panic(err)
}
ch, err := conn.Channel()
if err != nil {
panic(err)
}
observ := observer.New(ch, json.NewCodec())
chOut, err := observ.Sub("serviceName", &CreateUserEvent{})
if err != nil {
panic(err)
}
for e := range chOut {
log.Printf("ID: %d, User name: %s", e.Data.(*CreateUserEvent).ID, e.Data.(*CreateUserEvent).Name)
}
}
```
## Pub
``` go
package main
import (
"log"
"github.com/streadway/amqp"
"github.com/l-vitaly/observer"
"github.com/l-vitaly/observer/json"
)
type CreateUserEvent struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
conn, err := amqp.Dial("amqp://rmqhost/")
if err != nil {
panic(err)
}
ch, err := conn.Channel()
if err != nil {
panic(err)
}
observ := observer.New(ch, json.NewCodec())
err = observ.Pub("serviceName", &CreateUserEvent{ID: 1, Name: "Hello World"})
if err != nil {
panic(err)
}
}
```