chameleonbr / node-red-contrib-redis

Node RED client for Redis with pub/sub, list, lua scripting and other commands support.
MIT License
46 stars 40 forks source link

Crashes node-red server if subscribed channel publishes non-JSON formatted message #5

Closed ttr453 closed 8 years ago

ttr453 commented 8 years ago

First off, get module! It's just what I needed.

The issue I face is that the module requires that the data you receive via SUBSCRIBE/PSUBSCRIBE or B[L|R]POP be in JSON format. If a message arrives from the redis server that is not in JSON format redis.js crashes and takes down the node-red server.

I propose that the message handlers drop this expectation. Instead simply put the message from redis in the payload as received and let the flow designers use node-red JSON nodes to convert message payloads if needed. The changes below stopped the crashing for me:

diff --git a/redis.js b/redis.js
index e1fb5ff..1111b8e 100644
--- a/redis.js
+++ b/redis.js
@@ -70,13 +70,13 @@ module.exports = function(RED) {
                     node.send({
                         pattern: pattern,
                         topic: channel,
-                        payload: JSON.parse(message)
+                        payload: message
                     });
                 });
                 node.client.on('message', function(channel, message) {
                     node.send({
                         topic: channel,
-                        payload: JSON.parse(message)
+                        payload: message
                     });
                 });
                 node.client[node.command](node.topics);

I haven't tried publishing anything with node-red-contrib-redis yet, but I expect a symmetrical assumption about JSON encoding exist. Similarly, I propose that you allow the flow designer to use JSON node-red nodes to format message payloads as JSON if they need this conversion, otherwise simply publish/push the message payload as received.

Again, thanks for the great node-red capability!

chameleonbr commented 8 years ago

Hi, I'm away from my computer right now, next week will be updating and generating a new version.