// Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 // Package nats contains NATS message publisher implementation. package nats import ( "context" "fmt" "github.com/gogo/protobuf/proto" "github.com/mainflux/mainflux" broker "github.com/nats-io/nats.go" ) 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(_ context.Context, _ string, msg mainflux.Message) error { data, err := proto.Marshal(&msg) if err != nil { return err } subject := fmt.Sprintf("channel.%s", msg.Channel) return pub.nc.Publish(subject, data) }