How can you "subscribe" to a Redis stream (5.0)?
See original GitHub issueHi,
I’ve implemented some of the commands to add (XADD) to and read (XREAD) from a Redis stream. However, what I’m not entirely clear about is how you can use these commands to subscribe to a stream like you would in PubSub?
For example, to get every new message that is added to a stream, should I send the XREAD command on an interval? Like so:
setInterval(async () => {
const command = new Redis.Command('XREAD', ['STREAMS', stream, lastId || '0']);
const events = await this.redis.sendCommand(command);
console.log('Redis events received: ', events);
if (events) cb(events[0]);
}, 1000);
Issue Analytics
- State:
- Created 5 years ago
- Reactions:16
- Comments:15 (4 by maintainers)
Top Results From Across the Web
Redis Streams tutorial
The Redis stream data type was introduced in Redis 5.0. ... in a stream, usually what we want instead is to subscribe to...
Read more >Redis Streams - Amazon AWS
Redis incorporated the publish-subscribe pattern in version 2.0.0. The pattern allows clients to subscribe to one or more channels and receive messages as...
Read more >Redis Streams – Redis 5.0's Newest Data Type - Alibaba Cloud
In this article, we will look at real-world applications of Redis Streams, a new data type introduced in Redis 5.0.
Read more >Getting Started with Redis Streams and Java
Lines 3-5 connect to Redis · Lines 7-10 create the message body, using a map, since Redis Streams messages are string key/values in...
Read more >Redis Streams Explained - YouTube
Redis Streams allow us to aggregate numerous sources of information into one easily consumable source of truth. Join Justin as we learn ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In the latest version (4.10.0), all stream-related methods are added. Parameter and reply formats of these methods are kept the same as their corresponding Redis commands:
Reply transformers are not applied for backward compatibility though, it’s not hard to implement your own one. Here’s a modification leaving out parts that is not necessary with 4.10.0 based on the awesome implementation from @vflopes :
For the question that @Ventis raised, you can use the block mode of
XREAD
to avoid fetching data periodically (The following example doesn’t have the transformers above applied):@mcleishk Should be released before June 20.