summaryrefslogtreecommitdiff
path: root/nats/notify.go
blob: d75928db1a3714236920f2d09e45fdc80d848380 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package nats

import (
	"github.com/nats-io/nats.go"
	"uvok.de/go/training_fellow/registration"
)

type NatsNotifier struct {
	ServerUrl string
}

func (notif *NatsNotifier) NotifyNewRegistration(reg *registration.Registration) error {
	conn, err := nats.Connect(notif.ServerUrl)
	if err != nil {
		return err
	}
	defer conn.Close()

	enc_conn, err := nats.NewEncodedConn(conn, nats.JSON_ENCODER)

	if err != nil {
		return err
	}

	defer enc_conn.Close()

	err = enc_conn.Publish(NATS_TOPIC_REGISTRATION_NEW, reg)
	if err != nil {
		return err
	}
	return nil
}