Skip to content
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ queue.add('Hello, World!', (err, id) => {
})
```

Using Promises

```js
// Message with payload 'Hello, World!' added.
// 'id' is returned, useful for logging.
var id = await queue.add('Hello, World!');
```

Get a message from the queue:

```js
Expand All @@ -53,6 +61,16 @@ queue.get((err, msg) => {
})
```

Using Promises

```js
var msg = await queue.get();
console.log('msg.id=' + msg.id)
console.log('msg.ack=' + msg.ack)
console.log('msg.payload=' + msg.payload) // 'Hello, World!'
console.log('msg.tries=' + msg.tries)
```

Ping a message to keep it's visibility open for long-running tasks

```js
Expand All @@ -62,6 +80,14 @@ queue.ping(msg.ack, (err, id) => {
})
```

Using Promises

```js
// Visibility window now increased for this message id.
// 'id' is returned, useful for logging.
var id = await queue.ping(msg.ack);
```

Ack a message (and remove it from the queue):

```js
Expand All @@ -71,6 +97,14 @@ queue.ack(msg.ack, (err, id) => {
})
```

Using Promises

```js
// This msg removed from queue for this ack.
// The 'id' of the message is returned, useful for logging.
var id = await queue.ack(msg.ack);
```

By default, all old messages - even processed ones - are left in MongoDB. This is so that
you can go and analyse them if you want. However, you can call the following function
to remove processed messages:
Expand All @@ -81,6 +115,14 @@ queue.clean((err) => {
})
```

Using Promises

```js
// All processed (ie. acked) messages have been deleted
await queue.clean();
})
```

And if you haven't already, you should call this to make sure indexes have
been added in MongoDB. Of course, if you've called this once (in some kind
one-off script) you don't need to call it in your program. Of course, check
Expand All @@ -92,6 +134,13 @@ queue.createIndexes((err, indexName) => {
})
```

Using Promises

```js
// The indexes needed have been added to MongoDB.
var indexName = await queue.createIndexes();
```

## Creating a Queue ##

To create a queue, call the exported function with the `MongoClient`, the name
Expand Down Expand Up @@ -172,6 +221,13 @@ var queue = mongoDbQueue(db, 'queue', { delay : 10 })

This is now the default for every message added to the queue.

If you set `delay` to be a Date it will delay the message until that date.

To delay all messages until January 1st 2077, try this:
```
var queue = mongoDbQueue(db, 'queue', { delay : new Date('2077-01-01') })
```

### deadQueue - Dead Message Queue ###

Default: none
Expand Down
Loading