-
-
Save davalapar/56121fe3a2bf032b58ae39c3e8bb61c2 to your computer and use it in GitHub Desktop.
Example of using Redis Streams with Javascript/ioredis
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Redis = require('ioredis'); | |
var redis = new Redis({ | |
host: "nodesydney.wiftycloud.com", | |
password: "7884b8baaa49fbcb48f17ad2a146" | |
}); | |
async function main() { | |
// write an event to stream 'events', setting 'key1' to 'value1' | |
await redis.sendCommand( | |
new Redis.Command("XADD", ["queue", "*", "message", "NodeJS"])); | |
// read events from the beginning of stream 'events' | |
let res = await redis.sendCommand( | |
new Redis.Command("XREAD", ["STREAMS", "queue", 0])); | |
// parse the results (which are returned in quite a nested format) | |
let events = res[0][1]; | |
for (var i=0; i<events.length; i++) { | |
let thisEvent = events[i] | |
console.log("## id is ", thisEvent[0].toString()); | |
for (var eachKey in thisEvent[1]) { | |
console.log(thisEvent[1][eachKey].toString()); | |
} | |
} | |
redis.disconnect() | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment