Skip to main content

Realtime Updates

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


Let's assume your

SDK 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.

Code Sample

import time
import threading
import pprint
from c8 import C8Client

# Variables - URLs

# Variables - DB
EMAIL = "[email protected]"
PASSWORD = "xxxxx"
FABRIC = "_system"

# Variables - Data
data = [
{"ip": "", "action": "block", "rule": "blacklistA"},
{"ip": "", "action": "block", "rule": "blacklistA"},
{"ip": "", "action": "block", "rule": "blacklistB"},
{"ip": "", "action": "block", "rule": "blacklistA"},
{"ip": "", "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(f"\n1. CONNECT: federation: {GLOBAL_URL}, user: {EMAIL}")
client = C8Client(protocol='https', host=GLOBAL_URL, port=443,
email=EMAIL, password=PASSWORD, geofabric=FABRIC)

# Step2: Create a collection if not exists
print(f"\n2. CREATE_COLLECTION: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")
if client.has_collection(COLLECTION_NAME):
collection = client.collection(COLLECTION_NAME)
collection = client.create_collection(COLLECTION_NAME, stream=True)

# Events are received by subscriber when changes are made to collection
def create_callback():
def callback_fn(event):

client.on_change(COLLECTION_NAME, callback=callback_fn, timeout=15)

# Step3: Subscribe to receive documents in realtime (PUSH model)
print(f"\n3. SUBSCRIBE_COLLECTION: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")
rt_thread = threading.Thread(target=create_callback)
print(f"Callback registered for collection: {COLLECTION_NAME}")

# Step4: Subscribe to receive documents in realtime (PUSH model)
print(f"\n4. INSERT_DOCUMENTS: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")
client.insert_document(COLLECTION_NAME, document=data)

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

print(f"\n6. DELETE_DATA: region: {GLOBAL_URL}, collection: {COLLECTION_NAME}")