1

I'm trying to read data from the file as follows and send data to the client.

I can send a 20MB file in 30 seconds. But I have to send 20MB per second.

Am I doing something wrong?

Server.js

var File = require('File')
var FileReader = require('filereader'), fileReader = new FileReader();
fileReader.setNodeChunkedEncoding(true || false);
fileReader.readAsArrayBuffer(new File('../Test.txt'));

fileReader.addEventListener('load', function (ev) {
  JsonData = JSON.stringify(new Int16Array(ev.target.result));
});


var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);

io.on('connection', function(socket){
    console.log('a user connected');
    console.log("DateTime : " + getDateTime()); 

    socket.on("update", function(data){
      console.log("DateTime : " + getDateTime());
      socket.send(JsonData);
    });

    socket.send(JsonData);

  socket.on('message', function(msg){
    console.log('message: ' + msg);
  });

  socket.on('disconnect', function(){
    console.log('user disconnected');
  });
});

http.listen(9099, function(){
  console.log('listening on *:9099');
});

Client.js

var socket = io("http://localhost:9099");
socket.on('message', function(message) {
   var JsonData = JSON.parse(message);
   socket.emit("update", { data:"getData"});
});

Socket.io version : 2.2.0

Filereader version: 0.10.3

NodeJS version: 10.16.3

M.Demiral
  • 13
  • 6
  • Your code throws an error due to incompatible dependency of `File`. Why don't you use the built-in `fs` module instead of those very old packages (last updated 5 years ago!)? – Avraham Sep 20 '19 at 09:57
  • I read the file as arraybuffer. fileReader.readAsArrayBuffer() As far as I see, there is no such method in fs. – M.Demiral Sep 20 '19 at 12:47
  • `fs` reads the same way (`buffer`) by default. – Avraham Sep 22 '19 at 08:00
  • To your main question, it seems you read the entire file and then send it as is to client. This is not a good idea. Consider using a "stream" mechanism. For example see this answer: https://stackoverflow.com/questions/48627210/how-to-stream-data-over-socket-io-to-client – Avraham Sep 22 '19 at 08:02

0 Answers0