Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6889801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:12:08+00:00 2026-05-27T06:12:08+00:00

I am using node.js to query a MySQL database table every 10 seconds. Using

  • 0

I am using node.js to query a MySQL database table every 10 seconds. Using socket.io, after the database query every 10 secs, every connected browser will receive a unique batch of rows from that query.

Problem: With the way I implemented this, at every 10 second interval, there is a chance that more than 1 browsers receive the same set of rows. I used some checking out system (as shown below) to mark rows that have been sent to another browser already, but it does not seem to work all the time. How can I guarantee that each browser gets a unique set of rows each time?

In other words, the simultaneous querying of the db by the other browsers are faster than the locking of the rows!

Implementation

Every 10 seconds, the following occurs

  1. Node.js app querys the MySQL database table SELECT * FROM table WHERE checkout = 0
  2. If result is returned, node.js server io.sockets.send a message to all connected browser saying that rows are available
  3. All connected clients respond with a socket.emit('ready')
  4. Server receives the ready emit, querys the database again to select 3 rows, then update tables to set the checkout column of these 3 rows to be 1
  5. Server then emits the database query result to the client

Apparently it seems that it takes lesser time for the 2nd browser to cause a SELECT... query than for the first browser to update the rows with checkout = 1. Is there another way to do this queueing?

Screenshot

As you can see in the screenshot of 2 browsers open at the same time, often both browsers will get the same set of rows. The ids of the rows are console.loged as shown.
enter image description here

CODE

Node.js

io.sockets.on('connection', function(socket) {
    // Client ready to take jobs after receiving broadcast
    socket.on('ready', function() {
        getListings(function(listings) {
            socket.emit('job', listings);   // send jobs
        });
    });

});

var getListings = function(callback) {
    client.query('SELECT * FROM table ' + 
                'WHERE job_checkout = 0 ' +
                'ORDER BY listing_id ASC ' +
                'LIMIT 0, 3', 
                function(error ,results, fields) {
                    if (error) 
                        throw error;
                    // Checkout listing now!
                    checkoutListings(results);
                    callback(results);
                });
};

var checkoutListings = function(listings) {
    for (var i = 0; i < listings.length; i++) {
        checkoutListing(listings[i]);
    }
}

var checkoutListing = function(listing) {
    client.query('UPDATE table ' + 
                    'SET job_checkout = 1 ' +
                    'WHERE listing_id = ?',
                    [ listing.listing_id ]);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:12:08+00:00Added an answer on May 27, 2026 at 6:12 am

    update the rows before selecting them. eg

    update table 
    set job_checkout = uniqueNumber 
    where job_checkout = 0
    limit 10
    

    uniqueNumber could be a process/thread id, or browser client id, or something suitable in the node.js environment(I’m not familiar with it). Now select the rows with that unique job_checkout.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a node.js app using node-mysql to query a MySQL database. Problem: It
i want to insert the node manually to drupal database using mysql query. i
I am using node-mysql to update a MySQL database table from node.js. I want
I am loading data from the database using mysql drivers for node.js. https://github.com/felixge/node-mysql I
Currently, this is how I perform a query using node-mysql client.query( sql, function( error,
I have a Database with a category table using the Nested Approach in MySql.
I'm interested in using Node.js as a socket server for stream-based communication with tens
I'm working on a mysql query in a Drupal database that pulls together users
I'm trying to run a simple query with node-mysql like this: client.query(UPDATE mytable SET
I'm experiencing my first mysql INSERT tests and I'm using these tables: table employees

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.