From 38aaf70405e390dee097e2af8c7654f2d745a250 Mon Sep 17 00:00:00 2001 From: Nam Huynh Date: Thu, 2 Dec 2021 09:32:45 +0700 Subject: [PATCH] add method PublishWithResponse --- pubsub.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pubsub.go b/pubsub.go index 33232a0..b6bae11 100644 --- a/pubsub.go +++ b/pubsub.go @@ -19,6 +19,18 @@ func Publish(stream, subject string, payload []byte) error { return nil } +// PublishWithResponse ... +func PublishWithResponse(stream, subject string, payload []byte) (*nats.Msg, error) { + channel := combineStreamAndSubjectName(stream, subject) + + resp, err := natsJS.PublishAsync(channel, payload) + if err != nil { + msg := fmt.Sprintf("publish message to subject %s error: %s", channel, err.Error()) + return nil, errors.New(msg) + } + return resp.Msg(), nil +} + // Subscribe ... func Subscribe(stream, subject string, cb nats.MsgHandler) (*nats.Subscription, error) { channel := combineStreamAndSubjectName(stream, subject)