mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-01 13:48:56 +08:00
31 lines
676 B
Go
31 lines
676 B
Go
// Package nats contains NATS-specific message repository implementation.
|
|
package nats
|
|
|
|
import (
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/mainflux/mainflux"
|
|
broker "github.com/nats-io/go-nats"
|
|
)
|
|
|
|
const topic string = "src.coap"
|
|
|
|
var _ mainflux.MessagePublisher = (*natsPublisher)(nil)
|
|
|
|
type natsPublisher struct {
|
|
nc *broker.Conn
|
|
}
|
|
|
|
// NewMessagePublisher instantiates NATS message publisher.
|
|
func NewMessagePublisher(nc *broker.Conn) mainflux.MessagePublisher {
|
|
return &natsPublisher{nc}
|
|
}
|
|
|
|
func (pub *natsPublisher) Publish(msg mainflux.RawMessage) error {
|
|
data, err := proto.Marshal(&msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return pub.nc.Publish(topic, data)
|
|
}
|