Files
2026-03-26 11:22:42 -07:00

31 lines
537 B
Go

package nats
import (
"context"
"time"
gnats "github.com/nats-io/nats.go"
)
type Publisher struct {
conn *gnats.Conn
topic string
timeout time.Duration
}
func NewPublisher(url, topic string) (*Publisher, error) {
conn, err := gnats.Connect(url)
if err != nil {
return nil, err
}
return &Publisher{conn: conn, topic: topic, timeout: 5 * time.Second}, nil
}
func (p *Publisher) Close() {
p.conn.Close()
}
func (p *Publisher) Publish(ctx context.Context, data []byte) error {
return p.conn.Publish(p.topic, data)
}