Skip to content

Instantly share code, notes, and snippets.

@ferronrsmith
Forked from forkfork/ioredis_example.js
Created April 1, 2018 08:22
Show Gist options
  • Save ferronrsmith/f8412bfa5bcd477cdc2e001545725b20 to your computer and use it in GitHub Desktop.
Save ferronrsmith/f8412bfa5bcd477cdc2e001545725b20 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: "foooo.wiftycloud.com",
password: "5d4595050d541e2f1c37c731677a"
});
async function main() {
// write an event to stream 'events', setting 'key1' to 'value1'
await redis.sendCommand(
new Redis.Command("XADD", ["events", "*", "key1", "value1"]));
// read events from the beginning of stream 'events'
let res = await redis.sendCommand(
new Redis.Command("XREAD", ["STREAMS", "events", 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