1
0
mirror of https://github.com/mainflux/mainflux.git synced 2025-04-24 13:48:49 +08:00
Mainflux.mainflux/lora/nats/publisher.go
Nick Neisen 66487eda42 MF-788 - Remove date and minimize copyright comments (#876)
* Update copyright comment for go files

Signed-off-by: nwneisen <nwneisen@gmail.com>

* Update copyright in assortment of file types

Signed-off-by: nwneisen <nwneisen@gmail.com>

* Remove missed copyright date

Signed-off-by: nwneisen <nwneisen@gmail.com>
2019-10-07 16:14:47 +02:00

36 lines
798 B
Go

// 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/go-nats"
)
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.RawMessage) error {
data, err := proto.Marshal(&msg)
if err != nil {
return err
}
subject := fmt.Sprintf("channel.%s", msg.Channel)
return pub.nc.Publish(subject, data)
}