Math Tutor app

The math tutor app exposes mathematics functions using dataBridges rpc feature. The math tutor app will create an RPC server called MathServer and will expose math functions inside.

dataBridges supports 2 types of RPC interactions

  • 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 Math app will be exposing 2 math function as Unary RPC ( math.add, math.multiply) and 1 math function as server streaming RPC (AllMathFunction)

Exposing RPC server is supported only by dataBridges server library. Hence Tutor will be using databridges-sio-server-lib. dataBridges server sdk for NodeJS, Python, .Net allows you to design powerful realtime backend applications. These applications have access to extra dataBridges features than their corresponding client SDK's. Refer to https://dev.databridges.io/rpc/rpc.concepts.html for concepts.

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.tutor.app by clicking on this link

Dependencies

For this application, you will require to use databridges-sio-server-lib (dataBridges Server Library) with Node.js.

  • Install databridges client library
  • npm install databridges-sio-server-lib --save

Download the package.json by clicking on this link

Create math.tutor.app.js file and open it in your favorite editor.

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

/*
    The math tutor app exposes mathematics functions using dataBridges rpc feature.
    The math tutor app will create an RPC server called MathServer and will expose math functions inside.

    dataBridges supports 2 types of RPC interactions
        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 Math app will be exposing 2 math function as Unary RPC ( math.add, math.multiply) and 1 math function as server streaming RPC (AllMathFunction)


    Important Note : 
    Exposing RPC server is supported only by dataBridges server library. Hence Tutor will be using databridges-sio-server-lib.
    dataBridges server sdk for NodeJS, Python, .Net allows you to design powerful realtime backend applications. 
    These applications have access to extra dataBridges features than their corresponding client SDK's.

    Refer to https://dev.databridges.io/rpc/rpc.concepts.html for concepts.


*/


// Include dataBridges client library package
const dBridges = require('databridges-sio-server-lib');

// Initialize dataBridges client
const dbridge = new 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/server/v1/authenticate
// use environmental variables for greater security of your keys
dbridge.auth_url = '_____URL_____';

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

// lets create a sessionID
const sessionID = Math.random().toString(36).substring(2, 7);
console.log('sessionID is ' + sessionID);

// Initialize RPC Server Maths.
let mathTutor = dbridge.rpc.init('MathTutor');

// Define all exposed functions.
mathTutor.functions = function () {

    // Unary RPC: the client sends a single request and receives a single response.
    function multiply(inparameter, response) {
        const payload = JSON.parse(inparameter.inparam);
        const a = parseFloat(payload.a);
        const b = parseFloat(payload.b);
        console.log('Received multiply for  ' + payload.a + ' and  ' + payload.b);

        console.log('Sending ' + payload.a + ' x ' + payload.b);
        response.end(payload.a + ' x ' + payload.b + ' = ' + (a * b).toString() + ' from ' + sessionID);
    }

    // Unary RPC: the client sends a single request and receives a single response.
    function add(inparameter, response) {
        const payload = JSON.parse(inparameter.inparam);
        const a = parseFloat(payload.a);
        const b = parseFloat(payload.b);
        console.log('Received add for  ' + payload.a + ' and  ' + payload.b);

        console.log('Sending ' + payload.a + ' + ' + payload.b);
        response.end(payload.a + ' + ' + payload.b + ' = ' + (a + b).toString() + ' from ' + sessionID);
    }

    // Server streaming RPC: the client sends a single request and in return, the server sends a stream of messages.
    async function AllMathFunction(inparameter, response) {
        const payload = JSON.parse(inparameter.inparam);
        const a = parseFloat(payload.a);
        const b = parseFloat(payload.b);
        console.log('Received AllMathFunction for  ' + payload.a + ' and  ' + payload.b);

        console.log('Sending ' + payload.a + ' + ' + payload.b);
        response.next(payload.a + ' + ' + payload.b + ' = ' + (a + b).toString() + ' from ' + sessionID);

        console.log('Sending ' + payload.a + ' - ' + payload.b);
        response.next(payload.a + ' - ' + payload.b + ' = ' + (a - b).toString() + ' from ' + sessionID);

        console.log('Sending ' + payload.a + ' x ' + payload.b);
        response.next(payload.a + ' x ' + payload.b + ' = ' + (a * b).toString() + ' from ' + sessionID);

        console.log('Sending ' + payload.a + ' / ' + payload.b);
        response.end(payload.a + ' / ' + payload.b + ' = ' + (a / b).toString() + ' from ' + sessionID);
    }
    // Bind the exposed function to mathTutor RPC server.
    mathTutor.regfn('add', add);
    mathTutor.regfn('multiply', multiply);
    mathTutor.regfn('AllMathFunction', AllMathFunction);
}

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

//Bind to disconnected event, to get intimation about dataBridges network disconnection.
dbridge.connectionstate.bind("disconnected", () => {
    console.log('Disconnected from dataBridges real-time network');
});

//Bind to connected event, Once the dataBridges is connected successfully, RPC Registration and other activities can be performed.
dbridge.connectionstate.bind("connected", () => {
    try {
        // Register RPC Server with dBrdige network.
        mathTutor.register()
    } catch (err) {
        // Catch any registration error.
        console.log('MathServer registration with dataBridges network faced an exception : ', err.source, err.code, err.message)
    }
    // Bind to server.registration.fail, This will be triggered When any registration issue is raised.
    mathTutor.bind('dbridges:rpc.server.registration.fail', (payload, metadata) => {
        console.log('MathServer registration with dataBridges network faced an exception : ', payload.source, payload.code, payload.message)
    });
    // Bind to server.online, This will be triggered When MathServer is ready to serve.
    mathTutor.bind('dbridges:rpc.server.online', (payload, metadata) => {
        console.log('MathServer is now Online and ready for Math function processing ...')
    });
});

Save your file and run node math.tutor.app.js from the same folder where you have saved package.json.