question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async function for queue subscribe

See original GitHub issue

Is there a way to use async functions in subscribers? I’m looking for a way to do this:

let sub3 = await nc.subscribe('foo.baz.>', async (err, msg) => {
     // await async task to finish before taking next item in the queue...
}, {queue: 'A'});

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aricartcommented, Mar 29, 2019

Here’s an example of what that would look like:

https://gist.github.com/aricart/0e0641468694ac451592765feea84835

1reaction
aricartcommented, Mar 29, 2019

@haywirez thanks for the clarifications, you are effectively describing a work queue. Implicitly it seems that you require in-order processing. The payload I take it is very small. Really what you are looking for is store-and-forward, but have the client be in control of the ‘next’ message.

Just to make sure we are on the same page, a NATS subscription doesn’t hold any sort of queue in the server. So while you want to only ‘take’ the next message, all messages for the subscription are already in the client’s message buffer. If the client were to fail, the messages would be lost.

You could take a look at node-nats-streaming - that will get you somewhat close. You would have to configure the streaming server a bit differently from what a normal configuration is:

  • the channel storing messages received from one subject would have to have some age discard (streaming channels typically grow unbounded).
  • the client would make a durable subscription (likely in a queue group)
  • the client would have to set its ackWait to the maximum amount of time decoding would take + extra.
  • the client would have to specify to only get a single message until the previous message is acknowledged

Now your client can process the message, and when done processing ack it (if not acknoledged it will receive the message again, or if in a queue group, a different client could be assigned the message).

This will get you much closer to what you are trying to do, but there may be additional gotchas. @kozlovic can probably inject additional wisdom.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async function for queue subscribe · Issue #39 · nats-io/nats.ts
Is there a way to use async functions in subscribers? I'm looking for a way to do this: let sub3 = await nc.subscribe('foo.baz.>...
Read more >
JS async / await tasks queue - javascript - Stack Overflow
enqueue () returns a new Promise , that will be resolved(or rejected) at some point later. This Promise can be used to handle...
Read more >
03 - Exploring Async.js - async.queue - YouTube
... donating: https://www.paypal.me/pentacode We're going to explore async. queue function to help us process ... Don't forget to subscribe !
Read more >
Async - OpenFaaS
The queue-worker acts as a subscriber and deserializes the HTTP request and uses it to invoke the function directly. The asynchronous workflow can...
Read more >
Mutiny - Async for bare mortal - Quarkus
Subscribing triggers the operation. The subscription method can also define handlers: the id value on success, or a failure when the insertion fails....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found