Add pull subscribe #1

Merged
namhq1989 merged 12 commits from add-pull-subscribe into master 2022-03-18 04:28:50 +00:00
2 changed files with 25 additions and 2 deletions
Showing only changes of commit 688b8d9754 - Show all commits

View File

@ -13,7 +13,7 @@ func (js JetStream) Publish(stream, subject string, payload []byte) error {
_, err := js.instance.PublishAsync(channel, payload) _, err := js.instance.PublishAsync(channel, payload)
if err != nil { if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - publish message to subject %s error: %s", channel, err.Error()) msg := fmt.Sprintf("[NATS JETSTREAM] - publish message to subject #%s error: %s", channel, err.Error())
return errors.New(msg) return errors.New(msg)
} }
return nil return nil
@ -30,3 +30,26 @@ func (js JetStream) Subscribe(stream, subject string, cb nats.MsgHandler) (*nats
} }
return sub, nil return sub, nil
} }
// PullSubscribe ...
//
// Example:
//
// sub, err := natsio.PullSubscribe("A_STREAM", "A_OBJECT")
//
// for {
// messages, err := sub.Fetch(10)
// // process each messages
// }
//
func (js JetStream) PullSubscribe(stream, subject string) (*nats.Subscription, error) {
channel := combineStreamAndSubjectName(stream, subject)
sub, err := js.instance.PullSubscribe(channel, subject)
if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - pull subscribe subject #%s error: %s", channel, err.Error())
return nil, errors.New(msg)
}
return sub, nil
}

View File

@ -13,7 +13,7 @@ func (js JetStream) QueueSubscribe(stream, subject, queueName string, cb nats.Ms
_, err := js.instance.QueueSubscribe(channel, queueName, cb) _, err := js.instance.QueueSubscribe(channel, queueName, cb)
if err != nil { if err != nil {
msg := fmt.Sprintf("[NATS JETSTREAM] - queue subscribe with subject %s error: %s", channel, err.Error()) msg := fmt.Sprintf("[NATS JETSTREAM] - queue subscribe with subject #%s error: %s", channel, err.Error())
return errors.New(msg) return errors.New(msg)
} }
return nil return nil