How to handle concurrent file write requests to node server with socket.io

How to handle simultaneous requests to write a file to a node server with socket.io. I use this to write:

fs.writefile('abc.txt','datatobewritten','utf8',function(err){}); 

I have an abc.txt file and suppose that two users try to write at the same time in this file, then I get an error message, because I have a queue for several requests.

+6
source share
2 answers

You need to sync records.

For a single instance of nodejs, you can use a simple queue, for example:

 module.exports = function(path, content, cb){ var queue = queues[path]; if (queue == null) queue = queues[path] = new Queue; queue.add(path, content, (err) => { cb(err); queue.next(); }); }; var fs = require('fs'); var queues = {}; class Queue { constructor () { this.queue = []; } next () { if (this.queue.length === 0) return; var [path, content, cb] = this.queue[0]; fs.writeFile(path, content, 'utf8', (err) => { this.queue.unshift(); cb(err); }); } add (...args) { this.queue.push(...args); if (this.queue.length === 1) { this.next(); } } } 

In a multi-process instance, you should use some kind of lock, like lockfile .

 var lockFile = require('lockfile'); var fs = require('fs'); module.exports = function(path, content, cb) { lockFile.lock('foo.lock', function (err) { if (err) return cb(err); fs.writeFile(path, content, cb); lockFile.unlock('foo.lock'); }); } 

For best performance, you can combine 2 approaches here.

+8
source

You should write the logs module names or whatever you like.

logs.js

 var fs = require('fs'); var writeStream = fs.createWriteStream('./abc.txt'); module.exports = { /* PUSH */ write: function (message, cb) { writeStream.write(message, cb); } } 

Then in your socket.io-related module, only the module on top is required, for example

var logs = require('./logs');

and write a message like this in socket.io callbacks! :)

logs.write('datatobewritten');

Bottom line "use fs.createWriteStream instead of fs.writefile "

Hope this makes sense :)

0
source

All Articles