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)
}
Menu