Skip to main content

Pub-Sub with Streams

This is about how to create geo-replicated streams and do queues & pub-sub messaging with local latencies across the globe.

Pre-requisite

Let's assume your

Installation

With Yarn or NPM

yarn add jsc8
(or)
npm install jsc8

If you want to use the driver outside of the current directory, you can also install it globally using the `--global` flag:

npm install --global jsc8

From source,

git clone https://github.com/macrometacorp/jsc8.git
cd jsC8
npm install
npm run dist

Code Sample

const jsc8 = require('jsc8');

const gdnUrl = "https://gdn.paas.macrometa.io";

// Crete a authenticated instance with Token / Apikey
// const client = new jsc8({url: gdnUrl, token: "XXXX", fabricName: '_system'});
// const client = new jsc8({url: gdnUrl, apiKey: "XXXX", fabricName: '_system'});
// await console.log("Authentication done!!...");

// Or use Email & Password to Authenticate client instance
const client = new jsc8(gdnUrl);

await client.login("[email protected]", "xxxxxx");

//Variables
const msgs = ["message 1", "message 2", "message 3"];
let numberOfMessages = 0;

async function getDCList() {
let dcListAll = await client.listUserFabrics();
let dcListObject = await dcListAll.find(function(o) { return o.name === geo_fabric; });
return dcListObject.options.dcList.split(",");
}


async function publish(stream) {
console.log("\n ------- PUBLISH MESSAGES ------");
const publisher = await client.createStreamProducer("testStream");
}

async function receive(stream) {
const consumer = await client.createStreamReader("testStream", "my-subscription");
consumer.on("message", (msg) => {
console.log(msg);
});
}

(async function() {
const dcList = await getDCList();
await console.log("dcList: ", dcList);

await client.createStream("testStream", false);
//Here the last boolean value tells if the stream is local or global. false means that it is global.


// publishing streams
await receive();

await publish();

})();