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 Node.js.
- Install databridges client library
npm install databridges-sio-client-lib --save
Download the package.json by clicking on this link
Create math.student.app.js
file and open it in your favorite editor.
Copy paste below code and you are rseady 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.
*/
// Include dataBridges client library package
const dBridges = require('databridges-sio-client-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/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) => {
console.log('\n', '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('\n', 'Disconnected from dataBridges real-time network');
});
dbridge.connectionstate.bind("connected", () => {
console.log('\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.
console.log('\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", () => {
console.log('\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) => {
console.log('\n', 'Received response for mathFunctions.call(add) : ', response)
}).catch((err) => {
// Catch exception for .call
console.log('\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) => {
console.log('\n', 'Received response for mathFunctions.call(multiply) : ', response)
}).catch((err) => {
// Catch exception for .call
console.log('\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) => {
console.log('\n', 'Received response for mathFunctions.call(AllMathFunction) : ', response)
}).then((response) => {
console.log('\n', 'FINAL response for mathFunctions.call(AllMathFunction) : ', response)
}).catch((err) => {
// Catch exception for .call
console.log('\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) => {
console.log('\n', 'MathTutor RPC server faced an issue : ', payload.source, payload.code, payload.message)
});
});
Save your file and run node math.student.app.js
from the same folder where you have saved package.json
.