Ok 🆒, we have already written messages using our Saga API. at some point
these messages replicate and arrive at each connected peer.
...But we also need to have the power to read the messages of our database. And
not only that, we will need to read those messages in real-time. We want
Saga to detect when the user receives/writes a message in the chat.
The next exercise achieves this objective: to read messages in real-time.
Saganow has a methodinitialize, we need that once our db isreadyour db updates the messages. This means, read the last messages from the hyperdb and add those to our messageMap.- Once we have this list, we should call the function that listens to the new changes in the db and en nuestra db y ante un cambio llame nuevamente al update de mensajes. De esta forma resolveríamos el problema de real-time.
- Each new message that
Sagaencounters would emit a newmessageevent with the key of the message as arguments. - We will find a way to be efficient when updating messages, to finish the update when it seems necessary. We don't want to werun the entire stream each time there is a change.
- To read the hyperdb we recommend you use
createHistoryStreamand iterate over the messages in reverse order. - Now
Sagaextends EventEmitter, that means that it has all the functionality to emit events. - When we want to finalize a stream we can call
destroy(). If the stream forms part of a chain of connected streams withpump, callingdestroywould cause each connected stream to end. - Remember that
pumpaccepts as it's final parameter a function that will be executied when all the streams end. - Don't forger to have at hand the documentation of hyperdb

- You can ask us anything! 🌈
$ npm test ./07