Skip to content

Commit 129ac12

Browse files
darkgl0wEomm
authored andcommitted
Update redis dep to streams compatibility (#32)
1 parent da53547 commit 129ac12

File tree

2 files changed

+64
-8
lines changed

2 files changed

+64
-8
lines changed

README.md

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ You can access the *Redis* client via `fastify.redis`. The client is
1616
automatically closed when the fastify instance is closed.
1717

1818
```js
19-
const fastify = require('fastify')
19+
'use strict'
20+
21+
const fastify = require('fastify')()
2022

2123
fastify.register(require('fastify-redis'), { host: '127.0.0.1' })
2224

@@ -46,7 +48,9 @@ the client is not automatically closed when the Fastify instance is
4648
closed.
4749

4850
```js
49-
const fastify = Fastify()
51+
'use strict'
52+
53+
const fastify = require('fastify')()
5054
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
5155

5256
fastify.register(fastifyRedis, { client: redis })
@@ -61,7 +65,9 @@ fastify.register(fastifyRedis, { client: redis })
6165
By using the `namespace` option you can register multiple Redis client instances.
6266

6367
```js
64-
const fastify = require('fastify')
68+
'use strict'
69+
70+
const fastify = require('fastify')()
6571
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
6672

6773
fastify
@@ -118,6 +124,56 @@ fastify.listen(3000, function (err) {
118124

119125
```
120126

127+
## Redis streams (Redis 5.0 or greater is required)
128+
129+
`fastify-redis` supports Redis streams out of the box.
130+
131+
```js
132+
'use strict'
133+
134+
const fastify = require('fastify')()
135+
136+
fastify.register(require('fastify-redis'), {
137+
host: '127.0.0.1',
138+
port: 6380
139+
})
140+
141+
fastify.get('/streams', async (request, reply) => {
142+
// We write an event to the stream 'my awesome fastify stream name', setting 'key' to 'value'
143+
await fastify.redis.xadd(['my awesome fastify stream name', '*', 'hello', 'fastify is awesome'])
144+
145+
// We read events from the beginning of the stream called 'my awesome fastify stream name'
146+
let redisStream = await fastify.redis.xread(['STREAMS', 'my awesome fastify stream name', 0])
147+
148+
// We parse the results
149+
let response = []
150+
let events = redisStream[0][1]
151+
152+
for (let i = 0; i < events.length; i++) {
153+
const e = events[i]
154+
response.push(`#LOG: id is ${e[0].toString()}`)
155+
156+
// We log each key
157+
for (const key in e[1]) {
158+
response.push(e[1][key].toString())
159+
}
160+
}
161+
162+
reply.status(200)
163+
return { output: response }
164+
// Will return something like this :
165+
// { "output": ["#LOG: id is 1559985742035-0", "hello", "fastify is awesome"] }
166+
})
167+
168+
fastify.listen(3000, function (err) {
169+
if (err) {
170+
fastify.log.error(err)
171+
process.exit(1)
172+
}
173+
})
174+
```
175+
*NB: you will find more information about Redis streams and the relevant commands [here](https://redis.io/topics/streams-intro) and [here](https://redis.io/commands#stream).*
176+
121177
## Acknowledgements
122178

123179
This project is kindly sponsored by:

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "standard && tap test.js",
8-
"redis": "docker run -p 6379:6379 --rm redis:3.0.7"
8+
"redis": "docker run -p 6379:6379 --rm redis:5"
99
},
1010
"repository": {
1111
"type": "git",
@@ -26,14 +26,14 @@
2626
},
2727
"homepage": "https://github.com/fastify/fastify-redis#readme",
2828
"devDependencies": {
29-
"fastify": "^2.3.0",
29+
"fastify": "^2.5.0",
3030
"redis": "^2.8.0",
3131
"standard": "^12.0.1",
32-
"tap": "^12.6.6"
32+
"tap": "^12.7.0"
3333
},
3434
"dependencies": {
35-
"fastify-plugin": "^1.5.0",
36-
"ioredis": "^4.9.0"
35+
"fastify-plugin": "^1.6.0",
36+
"ioredis": "^4.10.0"
3737
},
3838
"greenkeeper": {
3939
"ignore": [

0 commit comments

Comments
 (0)