You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
Add stream examples. (#1830)
* Adds stream consumer and producer scripts to doc. * Updated build command example. * Adds stream producer example. * Adds basic stream consumer example. * Added isolated execution. * Update README.md * Update stream-consumer.js Co-authored-by: Leibale Eidelman <leibale1998@gmail.com>
This commit is contained in:
65
examples/stream-consumer.js
Normal file
65
examples/stream-consumer.js
Normal file
@@ -0,0 +1,65 @@
|
||||
// A sample stream consumer using the blocking variant of XREAD.
|
||||
// This consumes entries from a stream created by stream-producer.js
|
||||
|
||||
import { createClient, commandOptions } from 'redis';
|
||||
|
||||
async function streamConsumer() {
|
||||
const client = createClient();
|
||||
|
||||
await client.connect();
|
||||
|
||||
let currentId = '0-0'; // Start at lowest possible stream ID
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
let response = await client.xRead(
|
||||
commandOptions({
|
||||
isolated: true
|
||||
}), [
|
||||
// XREAD can read from multiple streams, starting at a
|
||||
// different ID for each...
|
||||
{
|
||||
key: 'mystream',
|
||||
id: currentId
|
||||
}
|
||||
], {
|
||||
// Read 1 entry at a time, block for 5 seconds if there are none.
|
||||
COUNT: 1,
|
||||
BLOCK: 5000
|
||||
}
|
||||
);
|
||||
|
||||
if (response) {
|
||||
// Response is an array of streams, each containing an array of
|
||||
// entries:
|
||||
// [
|
||||
// {
|
||||
// "name": "mystream",
|
||||
// "messages": [
|
||||
// {
|
||||
// "id": "1642088708425-0",
|
||||
// "message": {
|
||||
// "num": "999"
|
||||
// }
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
// ]
|
||||
console.log(JSON.stringify(response));
|
||||
|
||||
// Get the ID of the first (only) entry returned.
|
||||
currentId = response[0].messages[0].id;
|
||||
console.log(currentId);
|
||||
} else {
|
||||
// Response is null, we have read everything that is
|
||||
// in the stream right now...
|
||||
console.log('No new stream entries.');
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
streamConsumer();
|
||||
|
Reference in New Issue
Block a user