Skip to content

Commit

Permalink
Do not listen on all messages, only subscribe to the needed ones
Browse files Browse the repository at this point in the history
  • Loading branch information
repejota committed Jun 29, 2016
1 parent 8478fef commit 263d050
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 8 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,14 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/events", authMiddleware)

// Start nats handler, subscribe to all events
n.Subscribe(">", natsHandler)
// Start nats handler, subscribe to all events related with the monitor
n.Subscribe("monitor.user", natsHandler)
n.Subscribe("service.create", natsHandler)
n.Subscribe("service.delete", natsHandler)
n.Subscribe("service.create.done", natsHandler)
n.Subscribe("service.create.error", natsHandler)
n.Subscribe("service.delete.done", natsHandler)
n.Subscribe("service.delete.error", natsHandler)

// Start Listening
addr := fmt.Sprintf("%s:%s", host, port)
Expand Down
6 changes: 4 additions & 2 deletions nats.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
package main

import (
"time"

"github.com/nats-io/nats"
"github.com/r3labs/sse"
"log"
"time"
)

func natsHandler(msg *nats.Msg) {
Expand All @@ -25,9 +25,11 @@ func natsHandler(msg *nats.Msg) {
}
case "service.create", "service.delete":
// Create a new stream
log.Println("Creating stream for", notification.getServiceID())
s.CreateStream(notification.getServiceID())
case "service.create.done", "service.create.error", "service.delete.done", "service.delete.error":
// Remove a new stream when the build completes
log.Println("Closing stream for", notification.getServiceID())
go func(s *sse.Server) {
// Notifications appear out of order, wait for all notifications to come through before closing
time.Sleep(time.Second)
Expand Down

0 comments on commit 263d050

Please sign in to comment.