Coder Social home page Coder Social logo

Comments (6)

chilts avatar chilts commented on August 19, 2024

It doesn't do long polling currently, since I don't think MongoDB does long polling - and since we're dependent on that, this would be impossible.

I guess the best way to do this would be just 'poll' (rather than 'long poll') and the library could poll for you. e.g. every 5 seconds (opts.interval), for a maximum of 10 times (opts.maxtimes) or something like that.

I'd leave .get(opts, callback) as it was and perhaps add a .poll(opts, callback). Then .poll() could easily just call .get() internally and could also take the same opts.visibility option too which it also just passes on. How does that sound?

I'm not promising I'm going to implement it, though I think it's a good idea. Feel free to send a PR if you're keen. :)

from mongodb-queue.

shivanshu3 avatar shivanshu3 commented on August 19, 2024

I think you could implement long polling by storing the callback internally inside the mongodb-queue instance when the .get method is called. When .add is called later on, then the callback gets called (if it exists).

I haven't really looked at your code yet. I might try implementing it at some point.

from mongodb-queue.

chilts avatar chilts commented on August 19, 2024

This won't work (in general) since you could be using the MongoDB queue collection from any number of services. ie. your web service might be adding messages to the queue, and a background worker might be taking them off, therefore your suggestion here won't really work. (It won't really harm it either, but it won't do what you're expecting it to.)

Even if it was added like this, there should definitely be an upper limit amount of time to wait before returning undefined. You wouldn't want to wait forever.

from mongodb-queue.

shivanshu3 avatar shivanshu3 commented on August 19, 2024

Oh I see what you mean. I'm working on an email service and I was planning on using this queue for sending emails and retrying failed attempts. All of this happens on a server within a single Node process.

So this might be a very specific use case I guess? I think this feature would be useful if you're only planning on using this queue from a single service.

from mongodb-queue.

chilts avatar chilts commented on August 19, 2024

I think .poll() would be useful in general, however .longpoll() or the implementation you mentioned in two comment above (three comments ago) wouldn't be.

Anyway, feel free to close if you like. I may have a crack at .poll() sometime, or I may not. :)

from mongodb-queue.

coderofsalvation avatar coderofsalvation commented on August 19, 2024

I'm trying to use this polling function:

          var q = mongoDbQueue(db, 'queue')
          var handler = (err, id) => err ? console.error(err) : null                                            
          var poll                                                                                        
          poll = (cb) => {
              q.get( (err, msg) => {  
                  if( err || !msg ) return setTimeout( () => poll(cb), 500 )
                  msg.payload = JSON.parse(msg.payload)
                  cb(err, msg, function(){
                      q.ack(msg.ack, handler)                                                                                   
                      q.clean(handler) // cleanup 
                      poll(cb)         // get next message                                                
                  })
              })  
          }                             
          q.poll = poll

          q.poll( function(err,msg,next){
              // process message
              next()
          })

It's probably not perfect, but we'll see

from mongodb-queue.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.