After socket io listens to an event, it carries out relevant processing to get the data, and then broadcasts the data.

in node js, use socket io to monitor a query event, then operate the database, make related queries, and broadcast the query results in the monitoring event. The query operation is carried out by calling other modules. How can the data be returned to the listening event after the query module is completed?

Apr.03,2021

according to reason, your query method is called inside the event. Generally, this method is asynchronous. You can pass in a callback function to inform the event to continue broadcasting

socket.on('xx', function() {
    getDb(function(db) {
       socket.brocast('xxx', db) // 
    })
})

//getDb

function getDb(fn) {
    // 
    fn(err, db)
}
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3ea25-2c432.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3ea25-2c432.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?