Math Student app

The student.app consumes math functions exposed by MathTutor app.

You need to run the math tutor app [ NodeJS, Python, .NET].If you run multiple instances of math tutor app, dataBridges will automatically load-balance the rpc.function calls.

The student.app uses dataBridges RPC (call-response), functionality.

  • Unary RPC: the client sends a single request and receives a single response.
  • Server streaming RPC: the client sends a single request and in return, the server sends a stream of messages.

The student app will be consuming 2 Unary RPC functions :-> add, multiple. The student app will consume a single server streaming RPC function :-> AllMathFunctions and will receive 4 math operations reply.

We will be using databridges_sio_client_lib in student app.

Before trying this sample, follow the Setup and initialization . For more information, see the Channels Pub/Sub API reference documentation.

Download source

Download the math_student_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 math_student_app.py file and open it in your favorite editor.

Copy paste below code and you are ready to run this example.

#    The student.app consumes math functions exposed by MathTutor app.
#    The student.app uses dataBridges RPC (call-response), functionality.
#    You need to run the math tutor app [ NodeJS, Python, .NET].
#    If you run multiple instances of math tutor app, dataBridges will automatically load-balance the rpc.function calls.
#       
#    dataBridges supports 2 types of RPC interactions
#        Unary RPC: the client sends a request and receives a single response.
#        Server streaming RPC: the client sends a request and in return, the receives multiple responses.
#        
#    The student app will be consuming 2 Unary RPC functions :-> add, multiple
#                            consume a single server streaming RPC function :-> AllMathFunctions
#                            and will receive 4 math operations reply.
#        
#    We will be using databridges_sio_client_lib in student app.


import asyncio
import json 
import random

# Include dataBridges client library package 
from databridges_sio_client_lib import dBridges
from databridges_sio_client_lib.exceptions import dBError


class mathStudentApp():
    def __init__(self):
        self.dbridge = None
        self.mathFunctions = None
        self.inparam = 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 Add(self):
        # Unary RPC: The client sends a single request and receives a single response.
        p = await self.mathFunctions.call('add', json.dumps(self.inparam), 10000,None)

        def onResult(res): 
            print("Received response for mathFunctions.call(add) : ", res)

        def onError(res):
            #  Catch exception for .call
            print("Error was encountered while executing mathFunctions.call(add) : ", res.source, res.code, res.message, )

        p.then(onResult).catch(onError)

    async def Multiply(self):
        # Unary RPC: The client sends a single request and receives a single response.
        p = await self.mathFunctions.call('multiply', json.dumps(self.inparam), 10000,None)

        def onResult(res):
            print("Received response for mathFunctions.call(multiply) : ", res)

        def onError(res):
            #  Catch exception for .call
            print("Error was encountered while executing mathFunctions.call(multiply) : ", res.source, res.code, res.message, )

        p.then(onResult).catch(onError)

    async def AllMathFunctions(self):

        def progress(response):
            print("Received response for mathFunctions.call(AllMathFunction) : ", response);

        # Server streaming RPC: the client sends a request and in return, the receives multiple responses.
        p = await self.mathFunctions.call('AllMathFunction', json.dumps(self.inparam), 10000, progress)

        def onResult(res):
            print("FINAL response for mathFunctions.call(AllMathFunction) : ", res)

        def onError(res):
            #  Catch exception for .call
            print("Error was encountered while executing mathFunctions.call(AllMathFunction) : ", res.source, res.code, res.message, )

        p.then(onResult).catch(onError)


    async def connected(self):
        print("Connected to dataBridges real-time network !!!:") 

        try:
            # We need to inform dataBridges network that the app want to consume 'mathTutor's functions.
            self.mathFunctions =  await self.dbridge.rpc.connect("MathTutor")
        except dBError as dberror:
            # Connect errors will be handled here.
            print("Connecting to MathTutor RPC server faced an exception : ", dberror.source, dberror.code, dberror.message)

        if self.mathFunctions!= None:
            async def serverconnect_success(payload, metadata):
                print("dataBridges granted access to MathTutor RPC server")

                #  Create Random numbers for "a" and "b"
                self.inparam = {"a": random.random()*100 + 1 , "b": random.random()*100 + 1}

                await self.Add()
                await self.Multiply()
                await self.AllMathFunctions()

            #  We will bind to rpc server connection success event. On success, we will call remote functions.
            self.mathFunctions.bind("dbridges:rpc.server.connect.success", serverconnect_success)

            async def serverconnect_fail(payload, metadata):
                print("MathTutor RPC server faced an issue : ", payload.source, payload.code, payload.message)

            #  Bind to server.connect.fail event to understand if any issues in Math Server Connection.
            self.mathFunctions.bind("dbridges:rpc.server.connect.fail", serverconnect_fail)

    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______"

        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:
        mathStudent = mathStudentApp()
        asyncio.run(mathStudent.MainExecute())
    except Exception as e:
        print(e)

Save your file and run python3 math_student_app.py from the same folder.