Skip to content

Commit

Permalink
Improve stream example
Browse files Browse the repository at this point in the history
  • Loading branch information
cressie176 committed Jun 12, 2023
1 parent 072ca61 commit e9f3034
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
31 changes: 28 additions & 3 deletions examples/stream_queues/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,35 @@ These examples show how to use stream queues with the lib.

Send a message to a stream queue
```
./send_stream.js
node send_stream.js
```

Receive all the messages from stream queue:
```
./receive_stream.js
```
node receive_stream.js
```

Consumers can be configured to receive messages using an offset via the `x-stream-offset` argument. e.g.

```js
channel.consume(queue, onMessage, {
noAck: false,
arguments: {
'x-stream-offset': 'first'
}
});
```

RabbitMQ supports six different types of offset, however specifying them can be

| Offset Type | Example Value | Notes |
|-----------|------------------------------------------------------------------|-------|
| First | `{ 'x-stream-offset': 'first' }` | Start from the first message in the log |
| Last | `{ 'x-stream-offset': 'last' }` | Start from the last "chunk" of messages (could be multiple messages) |
| Next | `{ 'x-stream-offset': 'next' }` | Start from the next message (the default) |
| Offset | `{ 'x-stream-offset': 5 }` | a numerical value specifying an exact offset to attach to the log at |
| Timestamp | `{ 'x-stream-offset': { '!': 'timestamp', value: 1686519750 } }` | a timestamp value specifying the point in time to attach to the log at. The timestamp must be the number of seconds since 00:00:00 UTC, 1970-01-01. Consumers can receive messages published a bit before the specified timestamp. |
| Interval | `{ 'x-stream-offset': '1h' }` | the time interval relative to current time to attach the log at. Valid units are Y, M, D, h, m and s |


See https://www.rabbitmq.com/streams.html#consuming for more details
4 changes: 3 additions & 1 deletion examples/stream_queues/receive_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ const amqp = require('amqplib');
noAck: false,
arguments: {
/*
Here you can specify the offset: : first, last, next, offset and timestamp, i.e.
Here you can specify the offset: : first, last, next, offset, timestamp and interval, i.e.
'x-stream-offset': 'first'
'x-stream-offset': 'last'
'x-stream-offset': 'next'
'x-stream-offset': 5
'x-stream-offset': { '!': 'timestamp', value: 1686519750 }
'x-stream-offset': '1h'
The timestamp must be the desired number of seconds since 00:00:00 UTC, 1970-01-01
The interval units can be Y, M, D, h, m, s
*/
'x-stream-offset': 'first'
Expand Down

0 comments on commit e9f3034

Please sign in to comment.