Thursday, 05 September 2019 09:12

Socket.IO File Upload in SAP HANA XSA

Written by https://blogs.sap.com/2019/09/06/socket.io-file-upload-in-sap-hana-xsa/
Rate this item
(0 votes)

In this blog, we are going to build a file (.csv) upload app with NodeJS and Socket.io. In previous blog, I created the similar app but using XSJS lib in HANA

XSA. With websocket, I would like to overcome the gateway timeout issue when using HTTP post method.

Let’s get started by creating the SAP Cloud Platform Business Application with database, NodeJS and web module.

Database Module

Under the database module, create the zfileupload_dummy.hdbtable and insertData.hdbprocedure.

NodeJS Module

  • We need to install the socket.io libraries, in package.json under “dependencies” section, add the following lines:
    "socket.io": "^2.2.0", "socket.io-client": "^2.2.0"​
  • In the server.js under NodeJS module (srv), attach socket.io to NodeJS HTTP server and create additional routes to handle the incoming socket from the client.
    //Start the Server server.on("request", app); // use socket.io var io = require('socket.io').listen(server); // define interactions with client io.sockets.on('connection', function(socket){ //Setup Additonal Node.js Routes require("./router")(app, server, socket); });​
  • Create a NodeJS routes, myNode.js.
  • In myNode.js we put some logic to listens to the incoming socket from the client “client_data” and then parse the content.
    sock.on('client_data', function(data) { //console.log(data.letter); var params = data.letter.split(","); var MATERIAL_NUMBER = params[0].toString(); var BATCH_DATE = params[1]; var MATERIAL_DESCRIPTION = params[2].toString(); var COUNTRY = params[3].toString(); var PROCESS_FLAG = params[4].toString(); var RUNID = Number(params[5]); var inputParams = { MATERIAL_NUMBER: MATERIAL_NUMBER, BATCH_DATE: BATCH_DATE, MATERIAL_DESCRIPTION: MATERIAL_DESCRIPTION, COUNTRY: COUNTRY, PROCESS_FLAG: PROCESS_FLAG, RUNID: RUNID }; ... });​
  • And call the store procedure insertData to insert into table zfileupload_dummy with @sap/hdbext lib. Refer to SAP Help for more details.
    let client = require("@sap/hana-client"); //Lookup HANA DB Connection from Bound HDB Container Service const xsenv = require("@sap/xsenv"); let hanaOptions = xsenv.getServices({ hana: { tag: "hana" } }); var hanaConfig = { host: hanaOptions.hana.host, port: hanaOptions.hana.port, user: hanaOptions.hana.user, password: hanaOptions.hana.password, CURRENTSCHEMA: hanaOptions.hana.schema }; var hdbext = require('@sap/hdbext'); hdbext.createConnection(hanaConfig, function(error, client) { if (error) { console.error(error); } hdbext.loadProcedure(client, null, "insertData", function(err, sp) { sp(inputParams, (err, parameters, results) => { if (err) { console.log("errB: " + err); } }); }); });​

Web Module

  • Create the module and update index.html.
  • Parse the uploaded .csv file and send the content to the server.
    let parsedata = []; var socket = io.connect(); let newLinebrk = data.split("\n"); for (let i = 1; i < newLinebrk.length; i++) { parsedata.push(newLinebrk[i].split(",")) socket.emit('client_data', {'letter': newLinebrk[i] }); }​

All setup is done. Now we can run all the modules and upload a .csv file in this format:

F000000001,,Material A,AA,AA,1 F000000002,,Material B,BB,AA,2 F000000003,,Material C,CC,AA,3 ---

Reference:

Read 163 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.