39 lines
775 B
Go
39 lines
775 B
Go
//
|
|
// Copyright (c) 2018
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
// Package nats contains NATS message publisher implementation.
|
|
package nats
|
|
|
|
import (
|
|
"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(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)
|
|
}
|