Pub / Sub app
Code a Pub/Sub app to publish ‘greetings ’ message. The app will also receive the greetings message from other app instances. We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances.
Before trying this sample, follow the Setup and initialization . For more information, see the Channels Pub/Sub API reference documentation.
Download source
Download the pub_sub_app by clicking on this link
Dependencies
For this application, you will require to use databridges_sio_client_lib (dataBridges Client Library) with python.
- Install databridges client library
pip3 install databridges_sio_client_lib
Create pub_sub_app.py
file and open it in your favorite editor.
Copy paste below code and you are ready to run this example.
# Code a Pub/Sub app to publish 'greetings ' message. The app will also receive the greetings message from other app instances.
# We can run 100's of these pub/sub app instances. Each of them will send a single greeting message and will receive greetings from all the other app instances.
import asyncio
import string
import random
# Include dataBridges client library package
from databridges_sio_client_lib import dBridges
from databridges_sio_client_lib.exceptions import dBError
class pubSubApp():
def __init__(self):
self.dbridge = None
self.helloWorld = None
self.sessionID = None
# Bind to disconnected event, to get intimation about dataBridges network disconnection.
async def disconnected(self):
print("Disconnected from dataBridges real-time network:")
async def connected(self):
print("Connected to dataBridges real-time network !!!");
try:
# Subscribe to channel 'Hello-World',
self.helloWorld = await self.dbridge.channel.subscribe("Hello-World")
except Exception as dberror:
print("Channel subscription faced an exception: ", dberror.source, dberror.code, dberror.message)
if self.helloWorld != None:
# We will bind to subscription success event. On success, we will publish event messages.
async def subscribe_success(payload, metadata):
# we can also check if the helloWorld subscription isOnline.
if(self.helloWorld.isOnline()):
print("Publishing greetings ")
try:
await self.helloWorld.publish("greetings", "Greetings from " + self.sessionID)
except Exception as dberror:
print("Publishing messages faced an exception: ", dberror.source, dberror.code, dberror.message)
self.helloWorld.bind("dbridges:subscribe.success", subscribe_success)
async def subscribe_fail(payload, metadata):
print("Channel subscription faced an exception:", payload.source, payload.code, payload.message)
# Bind to Subscription failure event, to get notified if subscription to channel has been rejected by dataBridges network
self.helloWorld.bind("dbridges:subscribe.fail", subscribe_fail)
async def greetingEvent(payload, metadata):
print("received greeting message : ", payload)
# Bind to the channel.event that you want to process. Here we will bind to 'greetings'
self.helloWorld.bind("greetings", greetingEvent)
async def MainExecute(self):
# Initialize dataBridges client
self.dbridge = dBridges()
# Replace your application key below, which you have received from dataBridges management portal.
# use system variables for greater security of your keys
self.dbridge.auth_url = "_____URL_____"
# Replace your authentication url below, which you have received from dataBridges management portal.
# example https://endpoint01.databridges.io/client/v1/authenticate
# use system variables for greater security of your keys
self.dbridge.appkey = "____appKey______"
# lets create a sessionID
self.sessionID = ''.join(random.choices(string.ascii_lowercase + string.digits, k=7))
print("sessionID is " + self.sessionID);
self.dbridge.connectionstate.bind("connected", self.connected)
self.dbridge.connectionstate.bind("disconnected", self.disconnected)
# Connect to dataBridges. If any runtime error it will be caught in catch().
try:
await self.dbridge.connect()
except dBError as dberror:
print("dataBridges Connection exception..", dberror.source, dberror.code, dberror.message)
if __name__ == '__main__':
try:
pubSub = pubSubApp()
asyncio.run(pubSub.MainExecute())
except Exception as e:
print(e)
Save your file and run python3 pub_sub_app.py
from the same folder.