Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ You can access the *Redis* client via `fastify.redis`. The client is
automatically closed when the fastify instance is closed.

```js
const fastify = require('fastify')
'use strict'

const fastify = require('fastify')()

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

Expand Down Expand Up @@ -46,7 +48,9 @@ the client is not automatically closed when the Fastify instance is
closed.

```js
const fastify = Fastify()
'use strict'

const fastify = require('fastify')()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })

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

```js
const fastify = require('fastify')
'use strict'

const fastify = require('fastify')()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })

fastify
Expand Down Expand Up @@ -118,6 +124,56 @@ fastify.listen(3000, function (err) {

```

## Redis streams (Redis 5.0 or greater is required)

`fastify-redis` supports Redis streams out of the box.

```js
'use strict'

const fastify = require('fastify')()

fastify.register(require('fastify-redis'), {
host: '127.0.0.1',
port: 6380
})

fastify.get('/streams', async (request, reply) => {
// We write an event to the stream 'my awesome fastify stream name', setting 'key' to 'value'
await fastify.redis.xadd(['my awesome fastify stream name', '*', 'hello', 'fastify is awesome'])

// We read events from the beginning of the stream called 'my awesome fastify stream name'
let redisStream = await fastify.redis.xread(['STREAMS', 'my awesome fastify stream name', 0])

// We parse the results
let response = []
let events = redisStream[0][1]

for (let i = 0; i < events.length; i++) {
const e = events[i]
response.push(`#LOG: id is ${e[0].toString()}`)

// We log each key
for (const key in e[1]) {
response.push(e[1][key].toString())
}
}

reply.status(200)
return { output: response }
// Will return something like this :
// { "output": ["#LOG: id is 1559985742035-0", "hello", "fastify is awesome"] }
})

fastify.listen(3000, function (err) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
```
*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).*

## Acknowledgements

This project is kindly sponsored by:
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "standard && tap test.js",
"redis": "docker run -p 6379:6379 --rm redis:3.0.7"
"redis": "docker run -p 6379:6379 --rm redis:5"
},
"repository": {
"type": "git",
Expand All @@ -26,14 +26,14 @@
},
"homepage": "https://github.com/fastify/fastify-redis#readme",
"devDependencies": {
"fastify": "^2.3.0",
"fastify": "^2.5.0",
"redis": "^2.8.0",
"standard": "^12.0.1",
"tap": "^12.6.6"
"tap": "^12.7.0"
},
"dependencies": {
"fastify-plugin": "^1.5.0",
"ioredis": "^4.9.0"
"fastify-plugin": "^1.6.0",
"ioredis": "^4.10.0"
},
"greenkeeper": {
"ignore": [
Expand Down