2

I am currently facing an issue. Correct me if i am wrong. In my project, I have a NodeJS server having mongoDB database & an android application connected to it.

There are 4 android devices connected to it and when server publishes the list, it send an emit call to all android devices via Socket.IO. The moment android receives emit call, it calls the API to get the published list. Here the request calls (4 requests) comes to server at same time.

And here what the unexpected assignment occurs to devices. Unexpected in the sense that there are some parameters based on which the allocation should be done to devices. When only one device is online then this scenario works perfectly but when concurrent requests arrives from more that one device then sometimes the concerned problem occurs.

Is it possible to handle concurrent request in one at a time way?

Discussions/suggestions are welcome. Execution flow is here

        flowController.on('START', function () {
      // Uploading and storing the binary data as excel file 
    });

    flowController.on('1', function () {
      // Validation to check if file contain required sheet with name and all and convert the file into JSON format
    });

    flowController.on('2', function () {
      // Replacing the keys from the JSON to match the required keys 
    });

    flowController.on('3', function () {
      // Massive validation of records & seperating the records as per the activity
    });

    // Activity 1  
    flowController.on('4', function () {
      // Insert records of Activity 1  
    });

    // Activity 2  
    flowController.on('5', function () {
      // Insert records of Activity 2
    });

    // Activity 3  
    flowController.on('6', function () {
      // Insert records of Activity 3
    });

    // Activity 3  
    flowController.on('END', function () {
      // End
    });
4
  • Could you get the new published list first (server-side), and then do a universal emit out to your android devices passing along the server-side list with the emit? That way only one API request would be needed and each android device could receive the updated list. This could be the wrong strategy though, as I may not fully understand what your exact needs are and I could be overlooking something. Your server might send out: io.emit('updatePublishedList', publishedList) , and then socket.on('updatePublishedList', function(publishedList) { /* do stuff here with list */ } Commented Sep 13, 2017 at 9:35
  • Share some code. Commented Sep 13, 2017 at 9:47
  • @explorer i can not share full code as that is too big but i can share you the skeleton of calling. What is feel should be added here is to lock the execution of piece of code for second request when first request is already have accessed and is currently inside the execution cycle Commented Sep 13, 2017 at 10:05
  • Okay. Since NodeJS is single threaded, no request can overwrite another request data, ie: publishedList. I guess the problem that you are referring is on events 4,5,6. If that is correct & based on what I am able to understand, I don't see any problem in the existing code flow. Commented Sep 13, 2017 at 10:16

1 Answer 1

1

If you want to handle one request at a time, you can implement a queue.

const queue = [];
const processingQueue = false;

socketServer.on('connection', (connection) => {

  connection.on('SOME_EVENT', (data, callback) => {
    queue.push({data: data, callback: callback});
  });

});

function processQueue() {
    async.whilst(() => queue.length, (callback) => {
        var request = queue.shift()
        // process request
        callback();
    },
    (err, n) => {
        setTimeout(processQueue, 1);
    });
}
processQueue();

But, if you share some code, there might be a better solution for this problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Just want to correct you as i have not mentioned much about server side execution, The socket emit is to just let android devices know that there are new published lists available at server. Now android will call API if it has no previous published lists available. The above scenario is where devices are logged in and they have no lists and at the same time server send emit of new publishlists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.