0

I was trying to use Socket.io recently, and I have established a successful connection between client and server and it is working properly. But what I want with my solution is to log data every 5 secs. and display that updated data in chart.js every 5 sec.

Now, when I change the values of the data, it does get updated in console but for it to be displayed in web I have to refresh the page which generates another request as well as a new socket is generated, which affects 5-10 secs interval for logging.

And I believe if there are more than 1 user using it there would be multiple request which server won't be able to handle. As I tried using socket.io in setInterval which I figured out is wrong way.

To summarize, I just want to log data and display data every 5 secs. In which I don't have to refresh the web page and open multiple socket.io.

I have tried it using short-polling, long-polling and tried socket.io multiple times but I guess either I am not using it properly or I have missed something.

''' Javascript(nodejs)

io.on("connection",(socket)=>{
    console.log("User connected: "+socket.id)

    function getRegisterValues(){
        client.readHoldingRegisters(69, 20, function(err, data) {
                
            let l=[]
            const dataTypes=['Real','Real','Int','Real','Real','Int','Real','Real','Int','Real','Real','Int']
            var i=0
            var c=0
            while(i<data.buffer.length){
                let s=[]
                
                while(s.length<3){
                    if(dataTypes[c]=='Real'){
                        s.push(data.buffer.readFloatBE(i).toFixed(4))
                        i=i+4
                    }
                    if(dataTypes[c]=='Int'){
                        s.push(data.buffer.readInt16BE(i))
                        i=i+2
                    }
                    c+=1
                }
                l.push(s)
            }
            for(var j=0;j<l.length;j++){
                console.log("Array element: "+l[j])
            }
            
            pool.query(`Select ${x},id from modbus_plc1;`,(err,data)=>{
                if (err){
                    throw err
                }
                var numArray=[]
        
                let arr=Object.keys(data)
                
                var id=data.map(function(str) {
                    return parseInt(Object.values(str)[1])
                })
                var numArray=data.map(function(str) {
                    return parseInt(Object.values(str)[0])
                })
                socket.emit('allData',{allData:data})
            })


    
            pool.promise().query('insert into modbus_plc1(ND_5101_SCRE_Hz,ND_5101_SCRE_Amp,ND_5101_SCRE_Status,ND_5101_ARM_Hz,ND_5101_ARM_Amp,ND_5101_ARM_Status,ND_5102_SCRE_Hz,ND_5102_SCRE_Amp,ND_5102_SCRE_Status,ND_5102_ARM_Hz,ND_5102_ARM_Amp,ND_5102_ARM_Status) values ('+l[0][0]+','+l[0][1]+','+l[0][2]+','+l[1][0]+','+l[1][1]+','+l[1][2]+','+l[2][0]+','+l[2][1]+','+l[2][2]+','+l[3][0]+','+l[3][1]+','+l[3][2]+')')
            .then( ([row,fields])=>{
                      console.log(row)
                }) 
                .catch(console.log)
                //.then( ()=>pool.end() );
            
            socket.emit('message',{data:l})
        })
    }

    getRegisterValues()

   setInterval(()=>{getRegisterValues()},5000)
})

'''

Here's my code, I have used setInterval() method just to display the updated data on the webpage as the value wasn't reflecting on the webpage despite the value was updated in socket.io.

0 Answers0