Last updated: January 19th 2020 ( Improve this guide )
Embark’s companion library EmbarkJS comes with some convenient APIs to easily subscribe to and sending messages using messages protocols like Whisper. In this guide we’ll take a closer look how this works.
Make sure to read our guide in using EmbarkJS first.
By default Embark will initialize EmbarkJS with the provider configured at
config/communication.js. However if we are using EmbarkJS directly or wish to change the provider configuration at runtime, we can do so using the
We can subscribe to channels using the
listenTo() method by specifying a list of channel topics like this:
listenTo() returns an Observable that we can subscribe to. Observables work great if multiple values can be emitted over time, which is exactly the case for messages being emitted with Whisper. In other words, this is a long-living Observable, so it’s important to unsubscribe from it once we’re no longer interested in the data. Otherwise we’ll introduce memory leaks.
Sending messages can be done using the
sendMessage() method and it’s entirely up to use whether we want to send plain text messages or even objects.
Here’s how to send a plain text message to the
And this code snippet shows how to send an object structure:
Notice that a topic/channel name has to be at least 4 characters long. Whisper will otherwise emit an error on the subscription.
On topic arrays:
Array of topics are considered an AND. In Whisper you can use another array for OR combinations of several topics e.g
["topic1", ["topic2", "topic3"]] =>
topic1 AND (topic2 OR topic 3).