0

We are developing a message board application where users can interact with each other. The application allows attachments with the messages. We are using stream.pipe(fs.createWriteStream) to upload the attachments.

We saw a strange behavior. If we post a message with attachment once, the application works fine. But, second time without page refresh the application fails to post.

There is no problem posting repeatedly without attachments. We are fairly new with node.js and looking for your comments.

Here is the code snippet for the message posting:`

if(messg.haveAttached==1){
       ss(client).on('posting', function(stream, data5) {
          var currentdate = new Date();
          var currentTime = currentdate.getTime();
          var filename = (data5.reactionFilePath)+ currentTime + "_" +path.basename(data5.name);
          stream.pipe(fs.createWriteStream(filename)).on('finish', function() {
          messg['attachedFilePath'] = filename;
          Obj.save(messg,socketIOobj,client.id);
          });
       });
    }else{
       reactionData['attachedFilePath'] = '';
       Obj.save(messg,socketIOobj,client.id);
    }` 

The error looks like:

.../node_modules/socket.io-stream/lib/iostream.js:97

this.socket._read(this.id, size); ^

TypeError: Cannot read property '_read' of null

  • in the second attachment does it pass the if statement if(messg.haveAttached==1){ } ? – Fadi Abo Msalam May 24 '17 at 06:20
  • yes, this code is called whenever someone posts. –  May 24 '17 at 06:24
  • does the on on('finish' is called twice ?? – Fadi Abo Msalam May 24 '17 at 06:29
  • No, 'finish' is called only once here. –  May 24 '17 at 06:36
  • You cannot reuse the piped data, that has been sent already. And you cannot pipe a stream after its 'end'. So you cannot process same stream twice, and need two streams. check this link https://stackoverflow.com/questions/19553837/node-js-piping-the-same-readable-stream-into-multiple-writable-targets – Fadi Abo Msalam May 24 '17 at 06:39
  • if that is the issue you are having check the 2nd solution here https://stackoverflow.com/questions/14173488/how-to-pipe-one-readable-stream-into-two-writable-streams-at-once-in-node-js using zlib it might be easy and helpful in your case – Fadi Abo Msalam May 24 '17 at 06:47
  • I am calling "stream.pipe(fs.createWriteStream(filename))" for each attachment (only one attachment is allowed per message). Does not that create a new stream every time? I am not using the piped data. I think there is some problem with the closing the stream. –  May 24 '17 at 06:53

0 Answers0