PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] How to send JSON in Redis stream NodeJs

 September 05, 2022     javascript, node.js, redis, redis-streams     No comments   

Issue

I am creating one script where I want some dummy data to send to redis server using streams. For that, I am using "ioredis" module for Redis stream. Its working fine when I send simple key value structure i.e {a:"hello",b:"world"}.

But not working for Json array structure.

{
    "name": "abc",
    "ts": 123,
    "lists": [{
        "oil": "1",
        "food": "1,
        "item": "1"
    }]

}

It throws errors like

throw new Error(`Data type of property ${key} is not supported.`);

Solution

Redis Streams don't do JSON. They do allow key-value data to be associated with each event. Note that both the key and the value must be strings.

ioredis does this with variadic arguments for the keys and values. There's an example on the ioredis repo but here's the bit you probably care about:

client.xadd("user-stream", "*", "name", "John", "age", "20")

Node Redis has a different syntax that allows you to pass in a JavaScript object. But, that object must be flat and full of strings. There's an example on GitHub but here's the tl;dr:

client.xAdd('user-stream', '*', { name: "John", age: "20" })

Also, note, that in both cases, the function is async so you can await it if you like.

If you want to store JSON in an event in a Stream in Redis, you'll need to stringify it first:

const data = JSON.stringify({
  "name": "abc",
  "ts": 123,
  "lists": [{
    "oil": "1",
    "food": "1",
    "item": "1"
  }]
})

/* if you use ioredis */
client.xadd("user-stream", "*", "data", data)

/* if you use Node Redis */
client.xAdd('user-stream', '*', { data })

Hope this helps!



Answered By - Guy Royse
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing