1
votes

I have one HTTP triggered Azure functions that gets data from DataBase and sends it via TCP/IP to a local server written in Python. Localy everything works. But after I deploy the function to the Azure portal, I cannot connect to local server to send data there. How I can fix this?

Below is my code:

module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const net = require('net');

var Connection = require('tedious').Connection;
var Request = require('tedious').Request;  


// connect to DB
var config = {
    authentication: {
        options: {
            userName: '',
            password: '' 
        },
        type: 'default'
    },
    server: '', 
    options:
    {
        database: '', 
        encrypt: true,
    }
};  

if (req.body) {

    var connection1 = new Connection(config);
        connection1.on('connect', function(err){ 
        console.log("Connected to DataBase");

        var request1 = new Request("SELECT 
KeyFigure.id,KeyFigure.unit_id,KeyFigure.week,KeyFigure.daily_gain,
FROM KeyFigure INNER JOIN KeyFigureDelivery ON 
KeyFigure.id=KeyFigureDelivery.key_figure_id WHERE 
KeyFigureDelivery.delivered=0", function(err, rowCount, rows){  
            console.log(rowCount + ' row(s) returned');
            if (err) {
                var client_1 = new net.Socket();
                client_1.connect(9000, '127.0.0.1', function() {
                    console.log('Connected to Server');
                    client_1.write(JSON.stringify("No data to send...from 
KeyFigure"));//'Data was written to a Data Base.');
                    client_1.destroy(); //close()
                });
                console.log("Error in Database KeyFigure");
                connection1.close();
                console.log("Disconnected_1 because of Error");
            }
            if (rowCount == 0) {
                var client_2 = new net.Socket();
                client_2.connect(9000, '127.0.0.1', function() {
                    console.log('Connected to Server');
                    client_2.write(JSON.stringify("No data to send...from 
KeyFigure"));//'Data was written to a Data Base.');
                    client_2.destroy(); //close()
                });
                connection1.close();
                console.log("Disconnected_1 because of 0 Rows")
            }
        });  

        request1.on('row', function(columns) {
            var DataToSend = [];
            var rowdata = new Object()
            columns.forEach(function(column) {
                //console.log("%s\t%s", column.metadata.colName, 
column.value);
                //DataToSend.push([String(column.metadata.colName) + ":" 
+ String(column.value)])
                    rowdata[column.metadata.colName] = column.value;
                });
            DataToSend = rowdata
            var client1 = new net.Socket();
            client1.connect(9000, '127.0.0.1', function() {
                console.log('Connected to Server');
                client1.write(JSON.stringify(DataToSend));//'Data was 
written to a Data Base.');
                //client.destroy(); //close()
                client1.on('data', function(data) {
                    console.log('Received: ' + JSON.parse(data).id);
                    //client.destroy();
                    var dataIn1 = JSON.parse(data)

                    if (dataIn1.status == 200){
                        var connection_in1 = new Connection(config);
                        connection_in1.on('connect', function(err){ 
                            console.log("Connected");
                            var request_in1 = new Request("UPDATE 
KeyFigureDelivery SET delivered = 1 WHERE key_figure_id= 
"+dataIn1.id+";", function(err, rowCount, rows){  
                            //console.log(rowCount + ' row(s) returned'); 
UPDATE KeyFigureDelivery SET delivered = 1 WHERE key_figure_id= 
"+data+";"
                            if (err) {
                                console.log("Error in Database 
KeyFigure");
                            }
                            });  
                                request_in1.on('row', function(columns) {
                                    columns.forEach(function(column) {                    
                                    });
                                    context.res = {
                                        status: 201, 
                                        body: "Input Data is Valid and 
Accepted.\n"
                                    };
                                    connection_in1.close();      
                                });
                                connection_in1.execSql(request_in1);  
                        }); 
                    }
                    else {
                        console.log("Data was not delivered!!..from 
Server...KeyFigure")
                    } 
                });
                client1.on('error', function(err){
                    console.log("Error: "+err.message);
                });
                //client1.destroy();
                connection1.close();
            });
        });
        request1.on('doneInProc', function (rowCount, more, rows) { 
            connection1.close();
            console.log("Connection 1 closed...")
        });
        connection1.execSql(request1); 
    });
}
else{
context.res = {
        status: 400,
        body: "Please pass a name on the query string or in the request 
body."
    };

};

What I need is: (azure function) get data from Data Base, send it to a server, if data is valid - server sends a message to client (azure function), update table in data base. all this in one HTTP triggered Azure Function. How it is possible to do?

Thank you for your help

1
where do you expect should "local server" be? It obviously won't be 127.0.0.1 when deployed to an Azure Function.silent
i made a local server on my computer. and 127.0.0.1 is not the IP adress I have (just for safety wrote it here)Ievgen Perederieiev
See my answer below. Web sockets are currently not supportedsilent
forgot about error message I get...Error: connect EACCES 127.0.0.1:9000Ievgen Perederieiev

1 Answers

2
votes

Web sockets are not (currently) supported in Azure Functions. See this Github thread for details: https://github.com/Azure/Azure-Functions/issues/738

In summary, there are open issues around design, billing, and technical implementation. I think there are reasonable solutions to these issues and we just need to start working on them once this feature is high enough on our list of priorities.

If you have a real case, you should comment on that Github thread.