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-js-lib in student app.

Before trying this sample, follow the Setup and initialization . For more information, see the RPC | request-response 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-js-lib (dataBridges Client Library) and Socket.io client with javascript.

The dependencies are already embeded in the tag with skeleton file which is referred in "Setup and initialization" section.

Create math.student.app.html file using the skeleton and open it in your favorite editor.

Copy paste below code inside $(document).ready() jquery function and you are ready to run this example.

// Initialize dataBridges client
const dbridge = new dbridges.dBridges();

// Replace your application key below, which you have received from dataBridges management portal.
// use environmental variables for greater security of your keys
dbridge.appkey = '____appKey______';

// Replace your authentication url below, which you have received from dataBridges management portal.
// example https://endpoint01.databridges.io/client/v1/authenticate
// use environmental variables for greater security of your keys
dbridge.auth_url = '_____URL_____';


// Connect to dataBridges. If any runtime error it will be caught in catch().
dbridge.connect().catch((err) => {
    logit(['\n', 'dataBridges Connection exception..', err.source, err.code, err.message]);
});

//Bind to disconnected event, to get intimation about dbrdige network disconnection.
dbridge.connectionstate.bind("disconnected", () => {
    logit(['\n', 'Disconnected from dataBridges real-time network']);
});

dbridge.connectionstate.bind("connected", () => {
    logit(['\n', 'Connected to dataBridges real-time network !!!']);
    let mathFunctions = null;

    try {
        // We need to inform dataBridges network that the app want to consume 'mathTutor's functions.
        mathFunctions = dbridge.rpc.connect('MathTutor');
    } catch (err) {
        // Connect errors will be handled here.
        logit(['\n', 'Connecting to MathTutor RPC server faced an exception : ', err.source, err.code, err.message]);
    }

    // We will bind to rpc server connection success event. On success, we will call remote functions.
    mathFunctions.bind("dbridges:rpc.server.connect.success", () => {
        logit(['\n', 'dataBridges granted access to MathTutor RPC server']);

        // Create Random numbers for "a" and "b"
        const a = Math.floor(Math.random() * 100) + 1;
        const b = Math.floor(Math.random() * 100) + 1;
        const funParam = { a: a, b: b };

        // Unary RPC: The client sends a request and receives a single response.
        mathFunctions.call('add', JSON.stringify(funParam), 1000 * 60 * 2).then((response) => {
            logit(['\n', 'Received response for mathFunctions.call(add) : ', response]);
        }).catch((err) => {
            // Catch exception for .call
            logit(['\n', 'Error was encountered while executing mathFunctions.call(add) : ', err.source, err.code, err.message]);
        });

        // Unary RPC: The client sends a request and receives a single response.
        mathFunctions.call('multiply', JSON.stringify(funParam), 1000 * 60 * 2).then((response) => {
            // Last Response will be received here.
            logit(['\n', 'Received response for mathFunctions.call(multiply) : ', response]);
        }).catch((err) => {
            // Catch exception for .call
            logit(['\n', 'Error was encountered while executing mathFunctions.call(multiply) : ', err.source, err.code, err.message]);
        });

        // Server streaming RPC: the client sends a request and in return, the receives multiple responses.
        mathFunctions.call('AllMathFunction', JSON.stringify(funParam), 1000 * 60 * 2, (response) => {
            logit(['\n', 'Received response for mathFunctions.call(AllMathFunction) : ', response]);
        }).then((response) => {
            logit(['\n', 'FINAL response for mathFunctions.call(AllMathFunction) : ', response]);
        }).catch((err) => {
            // Catch exception for .call
            logit(['\n', 'Error was encountered while executing mathFunctions.call(AllMathFunction) : ', err.source, err.code, err.message]);
        });

    });

    // Bind to server.connect.fail event to understand if any issues in Math Server Connection.
    mathFunctions.bind("dbridges:rpc.server.connect.fail", (payload, metadata) => {
        logit(['\n', 'MathTutor RPC server faced an issue : ', payload.source, payload.code, payload.message]);
    }); 
}); 

Save your file and double click if you want to test using file system. If you want to host this file in any of your preferred web server, copy the file into the root folder and open it in browser to see the output.