https://github.com/welteki/natsrr
NATS Request-Reply with a familiar interface
https://github.com/welteki/natsrr
Last synced: 3 months ago
JSON representation
NATS Request-Reply with a familiar interface
- Host: GitHub
- URL: https://github.com/welteki/natsrr
- Owner: welteki
- Created: 2022-12-25T11:10:46.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-12-25T11:11:00.000Z (over 2 years ago)
- Last Synced: 2025-02-23T06:03:40.679Z (3 months ago)
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Usage example
```go
package mainimport (
"net/http"
"os"
"os/signal"
"syscall""github.com/nats-io/nats.go"
"github.com/welteki/natsrr"
)func main() {
url := nats.DefaultURLnc, _ := nats.Connect(url)
defer nc.Drain()mux := natsrr.NewSubjectMux()
mux.HandleFunc("greet.joe", func(r natsrr.Responder, msg *nats.Msg) {
r.Respond([]byte("Hello joe"))
})mux.HandleFunc("greet.sue", func(r natsrr.Responder, msg *nats.Msg) {
r.SetStatus(http.StatusAccepted)
r.Respond([]byte("Hello sue"))
})sub, _ := nc.Subscribe("greet.*", mux.MsgHandler())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)<-sigs
sub.Unsubscribe()
}
```### Send requests with the nats CLI
```bash
$ nats req 'greet.joe' ""11:58:54 Sending request on "greet.joe"
11:58:54 Received with rtt 251.976µs
11:58:54 Status: 200Hello joe
``````bash
$ nats req 'greet.sue' ""11:59:42 Sending request on "greet.sue"
11:59:42 Received with rtt 238.851µs
11:59:42 Status: 202Hello sue
``````bash
$ nats req 'greet.josh' ""11:56:32 Sending request on "greet.josh"
11:56:32 Received with rtt 371.151µs
11:56:32 Description: No messages
11:56:32 Status: 404
```