Skip to content

Instantly share code, notes, and snippets.

@davalapar
Forked from forkfork/ioredis_example.js
Created December 8, 2018 21:40
Show Gist options
  • Save davalapar/56121fe3a2bf032b58ae39c3e8bb61c2 to your computer and use it in GitHub Desktop.
Save davalapar/56121fe3a2bf032b58ae39c3e8bb61c2 to your computer and use it in GitHub Desktop.
Example of using Redis Streams with Javascript/ioredis
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