Realtime Updates

This tutorial is about using Macrometa GDN as a realtime database with local latencies across the globe.

Pre-requisite

Let's assume your

  • Tenant name is guest@macrometa.io and
  • User password is guest.

Driver download


pyC8 requires Python 3.5+. Python 3.6 or higher is recommended

To install pyC8, simply run

    $ pip3 install pyC8

or, if you prefer to use conda:

    conda install -c conda-forge pyC8

or pipenv:

    pipenv install --pre pyC8

Once the installation process is finished, you can begin developing applications in Python.

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

from c8 import C8Client
import threading
import pprint
import time

# Variables - URLs
global_url = "gdn1.macrometa.io"


# Variables - DB
email = "guest@macrometa.io"
password = "guest"
geo_fabric = "_system"
collection_name = "ddos"

# Variables - Data
data = [
    {"ip": "10.1.1.1", "action": "block", "rule": "blacklistA"},
    {"ip": "20.1.1.2", "action": "block", "rule": "blacklistA"},
    {"ip": "30.1.1.3", "action": "block", "rule": "blacklistB"},
    {"ip": "40.1.1.4", "action": "block", "rule": "blacklistA"},
    {"ip": "50.1.1.5", "action": "block", "rule": "blacklistB"},
  ]

pp = pprint.PrettyPrinter(indent=4)

if __name__ == '__main__':

  # Step1: Open connection to GDN. You will be routed to closest region.
  print("\n1. CONNECT: federation: {},  user: {}".format(global_url, email))
  client = C8Client(protocol='https', host=global_url, port=443)
  tenant = client.tenant(email, password)
  fabric = tenant.useFabric(geo_fabric)

  # Step2: Create a collection if not exists
  print("\n2. CREATE_COLLECTION: region: {},  collection: {}".format(global_url, collection_name))
  if fabric.has_collection(collection_name):
      collection = fabric.collection(collection_name)
  else:
      collection = fabric.create_collection(collection_name)

  # Subscriber to receive events when changes are made to collection.
  def create_callback():
    def callback_fn(event):
        pp.pprint(event)
        return
    fabric.on_change(collection_name, callback=callback_fn)

  # Step3: Subscribe to receive documents in realtime (PUSH model)
  print("\n3. SUBSCRIBE_COLLECTION: region: {},  collection: {}".format(global_url, collection_name))
  rt_thread = threading.Thread(target=create_callback)
  rt_thread.start()
  time.sleep(2)
  print("Callback registered for collection: {}".format(collection_name))

  # Step4: Subscribe to receive documents in realtime (PUSH model)
  print("\n4. INSERT_DOCUMENTS: region: {},  collection: {}".format(global_url, collection_name))
  collection.insert_many(data)

  # Step5: Wait to close the callback.
  print("\n5. Waiting to close callback")
  rt_thread.join()

  # Step6: Delete data.
  print("\n6. DELETE_DATA: region: {}, collection: {}".format(global_url, collection_name))
  collection.truncate()
  #fabric.delete_collection(collection_name)
'use strict'

const jsc8 = require('jsc8')

// Variables - DB
const global_url = "https://gdn1.macrometa.io"
const client = new jsc8(global_url)
const guest_password = "guest"
const geo_fabric = "_system"
const guest_email = "guest@macrometa.io"
const collection_name = "ddos"
let collectionDetails;
let collection_ddos = null

// Variables - Data
const data = [
    {"ip": "10.1.1.1", "action": "block", "rule": "blacklistA"},
    {"ip": "20.1.1.2", "action": "block", "rule": "blacklistA"},
    {"ip": "30.1.1.3", "action": "block", "rule": "blacklistB"},
    {"ip": "40.1.1.4", "action": "block", "rule": "blacklistA"},
    {"ip": "50.1.1.5", "action": "block", "rule": "blacklistB"},
  ]

async function login(){
  console.log(`\n 1. CONNECT: federation: ${global_url}`);
  await client.login(guest_email, guest_password);
  await client.useFabric(geo_fabric);
}

async function createCollection() {
  console.log("\n 2. CREATE_COLLECTION")

  try{
      collection_ddos = client.collection(collection_name)
      let exists_coll = await collection_ddos.exists()
      if (exists_coll === false) {
            collectionDetails = await collection_ddos.create()

        }


      // adding a onChange listner for collection
      collection_ddos.onChange({
        onmessage: (msg) => console.log("message =>", msg),
        onopen: () => {
          console.log("Collection " + collection_name + " opened   successfully")
        },
        onclose: () => console.log("connection closed")
      }, "gdn1.macrometa.io");

      }
    catch (e) {
    await console.log("Collection creation did not succeed due to " + e)
    }
}

async function insertData() {
  console.log(`\n 3. INSERT_DATA in region ${global_url}`)
  for (let i = 0; i < data.length; i++) { 
      await collection_ddos.save(data[i])
  }

  collection_ddos.closeOnChangeConnection()
}

async function deleteData(){
  console.log("\n 4. DELETE_DATA")
  await collection_ddos.truncate()
}

(async function(){
  await login();
  await createCollection();
  await insertData();
  await deleteData();
})();
Top