Search code examples
node.jsexpressraspberry-pihid

raspberry node-hid-stream: using multiple hid devices slowing down execution of application


i'm bulding an application on Raspberry PI 4 1GB RAM basing on webserver, reading data from hid devices and serialport using express, node-hid-stream and serialport libs. Everything works fine with 3 hid devices, but after adding the fourth device application is terribly slowing down(starting web server in 5-10minutes, serial port isn't even opening). Raspberry RAM consumption is around 700MB during running applications with 3 and 4 hid devices, only app is lagging, data from all hid devices is available.
According to node-hid documentation:

This is not a limitation of node-hid but a limitation of all user-space libraries. This is a security feature of the OS to prevent input device snooping.

Maybe this is a problem.

Some sample of code:

var express = require('express');
var socket = require('socket.io');
var app = express();
var server = app.listen(8080, function(){
    console.log('listening on port 8080');
    });
app.use(express.static('public'));

//Socket setup
var io = socket(server);

io.on('connection', function(socket){
    console.log('made socket connection')
    });

(...)

var serialport = require ("serialport");
var port = new serialport('/dev/ttyACM0', {
  baudRate: 9600,
   dataBits: 8,
   parity: 'none',
   stopBits: 1,
   flowControl: false,

});

port.on("open", function () {
  //console.log('port otwarty'); 
 port.on('data', function(data) {
    //console.log('data received: ' + data);
    f_CReaderCard(data.toString());
  });
});

(...)

var KeyboardCharacters = require('node-hid-stream').KeyboardCharacters;
var CReaderD1 = new KeyboardCharacters({path: '/dev/hidraw0'}); 
var CReaderD2 = new KeyboardCharacters({path: '/dev/hidraw1'}); 
var CReaderU1 = new KeyboardCharacters({path: '/dev/hidraw2'}); 
var CReaderU2 = new KeyboardCharacters({path: '/dev/hidraw3'}); 

(...)


Solution

  • Increase the thread number by adding this line on top

    process.env.UV_THREADPOOL_SIZE=10

    This will set the environment variable UV_THREADPOOL_SIZE to 10

    The default value is "4" and maximum is 1024