1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-05-02 22:17:10 +08:00

35 lines
868 B
Go
Raw Normal View History

// Package nats contains NATS-specific message repository implementation.
package nats
import (
"encoding/json"
"github.com/mainflux/mainflux/writer"
broker "github.com/nats-io/go-nats"
)
const topic string = "msg.coap"
var _ writer.MessageRepository = (*natsRepository)(nil)
type natsRepository struct {
nc *broker.Conn
}
// NewMessageRepository instantiates NATS message repository. Note that the
// repository will not truly persist messages, but instead they will be
// published to the topic and made available for persisting by all interested
// parties, i.e. the message-writer service.
func NewMessageRepository(nc *broker.Conn) writer.MessageRepository {
return &natsRepository{nc}
}
func (repo *natsRepository) Save(msg writer.RawMessage) error {
b, err := json.Marshal(msg)
if err != nil {
return err
}
return repo.nc.Publish(topic, b)
}