How to handle concurrent file write requests on a node server with socket.io. I am using this to write:
fs.writefile('abc.txt','datatobewritten','utf8',function(err){});
I have a file abc.txt and suppose two users try to write on the same time on this file then I am getting an error, so how do I queue multiple requests.
You have to synchronize the writes.
For a single instance of nodejs you can use simple queue, like this:
In multi-process instance you have to use some locking, for example with lockfile.
For better performance you can even combine 2 approaches here.
You should write a module names
logs
or what ever you like.Then in your socket.io related module just require the module at the top like
var logs = require('./logs');
and write message like this in the socket.io callbacks ! :)
logs.write('datatobewritten');
Hope this makes sense :)