add pull subscribe

This commit is contained in:
Nam Huynh 2022-03-18 10:06:11 +07:00
parent 04a8b182fb
commit 688b8d9754
2 changed files with 25 additions and 2 deletions

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