Skip to content

Instantly share code, notes, and snippets.

@forkfork
Last active April 13, 2021 17:41
Show Gist options
  • Save forkfork/c27d741650dd65631578771ab264dd2c to your computer and use it in GitHub Desktop.
Save forkfork/c27d741650dd65631578771ab264dd2c 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()
@cngeru
Copy link

cngeru commented Jul 30, 2020

Any way to use it with Socket.io

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment