User Guide
Everything you need to install, configure, and build with Montycat — from getting started to governance, advanced operations, and security.
Subscriptions
Subscription overview
Montycat provides a native subscription mechanism that allows clients to receive real-time notifications about changes in keyspaces.
Subscriptions can be set up for both in-memory and persistent keyspaces.
When a record is inserted, updated, or deleted, all clients subscribed to that keyspace are notified with the details of the change.
Create a subscription
To create a subscription, clients need to send a subscription request to Montycat, specifying the keyspace or even particular key they want to subscribe.
Subscriptions are not persistent. They will be lost if the client disconnects. Clients can re-subscribe to the same keyspace or key after reconnecting.
Note: Subscriptions can be created only for existing persistent keyspaces.
from keyspaces import Employees #keyspaces.py
def callback_on_data(data):
print("Received data:", data)
sub_task, stop_event = await Employees.subscribe(callback=callback_on_data)
# {"status": True, "message": "Subscription started", "payload": null, "error": null}
# on every insert/update in the keyspace: {
# "status": True,
# "message": "Key inserted/updated",
# "payload": {
# "__key__": "128222336824100726154851618391811195396",
# "__value__": {"username": "Name", "location": "Location", "age": 21}
# },
# "error": null
# }
# on every removal in the keyspace: {
# "status": True,
# "message": "Key removed",
# "payload": {
# "__key__": "128222336824100726154851618391811195396",
# },
# "error": null
# }Subscribe on a specific key
Clients can subscribe to a specific key within a keyspace by sending a subscription request to Montycat, specifying the key they want to monitor.
from keyspaces import Employees #keyspaces.py
async def callback_on_data(data):
print("Received data:", data)
subscription, stop_event = await Employees.subscribe(
key="128222336824100726154851618391811195396",
callback=callback_on_data
)Cancel a subscription
Clients can cancel their subscriptions at any time by sending a cancellation request.
from keyspaces import Employees #keyspaces.py
import asyncio
def callback(response):
print(f"Received data: {response}")
async def subscription():
sub_task, stop_event = await Employees.subscribe(callback=callback)
await asyncio.sleep(100)
stop_event.set() # stops the subscription after 100 secondsAllow / restrict subscriptions
Subscriptions can be governed engine-wide. Use these switches to allow or restrict clients from opening subscriptions to the database — for example to shed load or lock down a node.
# Engine-wide switch: allow clients to open subscriptions to this database.
await connection.allow_subscriptions()
# {"status": True, "payload": None, "error": None}
# Restrict subscriptions engine-wide (existing and new subscription attempts):
await connection.restrict_subscriptions()