From 4490f270e5763812b467182e03b9dc1346115612 Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Sun, 2 Jun 2019 15:19:19 +0200 Subject: [PATCH 1/7] Bump dependencies --- package.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 520842d..46481f1 100644 --- a/package.json +++ b/package.json @@ -26,14 +26,14 @@ }, "homepage": "https://github.com/fastify/fastify-redis#readme", "devDependencies": { - "fastify": "^2.3.0", + "fastify": "^2.4.1", "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": [ From 7d3c704d5a8a2755257dce8fddd2e8ed40a02f11 Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Sun, 2 Jun 2019 15:20:42 +0200 Subject: [PATCH 2/7] Fix code Adds a forgotten `return`. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 60fe3f5..a504fdf 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ function fastifyRedis (fastify, options, next) { fastify.redis[namespace] = client } else { if (fastify.redis) { - next(new Error('fastify-redis has already been registered')) + return next(new Error('fastify-redis has already been registered')) } else { if (!client) { try { From 8760bceaab7c60bc943cc2e1d3bcb88a9d77af30 Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Sat, 8 Jun 2019 11:39:28 +0200 Subject: [PATCH 3/7] Revert code fix (will submit another PR) --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a504fdf..60fe3f5 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,7 @@ function fastifyRedis (fastify, options, next) { fastify.redis[namespace] = client } else { if (fastify.redis) { - return next(new Error('fastify-redis has already been registered')) + next(new Error('fastify-redis has already been registered')) } else { if (!client) { try { From dfba3d20048e3c1cea4e128c8433298529357287 Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Sat, 8 Jun 2019 11:41:40 +0200 Subject: [PATCH 4/7] Adds documentation, bump docker image --- README.md | 61 +++++++++++++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 09c2bff..e5d9dbe 100644 --- a/README.md +++ b/README.md @@ -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' }) @@ -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 }) @@ -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 @@ -118,6 +124,55 @@ 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) + } +}) +``` + ## Acknowledgements This project is kindly sponsored by: diff --git a/package.json b/package.json index 46481f1..8b53ce3 100644 --- a/package.json +++ b/package.json @@ -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:latest" }, "repository": { "type": "git", From 9c109ee591e714656d29bbb38b52901041b6097c Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Sat, 8 Jun 2019 11:54:10 +0200 Subject: [PATCH 5/7] Add a link to Redis Streams doc --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e5d9dbe..ea25bb2 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,7 @@ fastify.listen(3000, function (err) { } }) ``` +*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 From 0a28e1a80b2925efab4add0ef077fca60edbe5ac Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Wed, 12 Jun 2019 13:19:34 +0200 Subject: [PATCH 6/7] Bump fastify to v2.5.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8b53ce3..e218cef 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ }, "homepage": "https://github.com/fastify/fastify-redis#readme", "devDependencies": { - "fastify": "^2.4.1", + "fastify": "^2.5.0", "redis": "^2.8.0", "standard": "^12.0.1", "tap": "^12.7.0" From 48fbf2637846c8536b631eb17448bc7e9fc2d3a4 Mon Sep 17 00:00:00 2001 From: darkgl0w <31093081+darkgl0w@users.noreply.github.com> Date: Thu, 13 Jun 2019 10:38:38 +0200 Subject: [PATCH 7/7] Set redis docker image to v5.x Co-Authored-By: Manuel Spigolon --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e218cef..06cfbe8 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "standard && tap test.js", - "redis": "docker run -p 6379:6379 --rm redis:latest" + "redis": "docker run -p 6379:6379 --rm redis:5" }, "repository": { "type": "git",