O'Reilly logo

Mastering Reactive JavaScript by Erich de Souza Oliveira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using Redis as the DataSource for messages

Now we must change our DataSource to use Redis instead of the basic subject from our original implementation. To do so, just change the file data_source.js, to use this implementation instead:

let Rx = require('rx'); let Redis = require('ioredis'); let subscriber = new Redis(); let publisher = new Redis(); const TOPIC = 'web_chat'; subscriber.subscribe(TOPIC); let messagesObservable = Rx.Observable.fromEvent(subscriber,'message',(channel,message)=>JSON.parse(message)); module.exports = {   push(data){     publisher.publish(TOPIC,JSON.stringify(data));   },   get(){     return messagesObservable;   } }; 

In this code we first import the RxJS and ioredis libraries; we then start two connections with the Redis ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required