'socket.io not accessing the value in node.js

I have the value to be updated in real-time using socket.io.The value is passed using event-emitter from backend to frontend and value is also passed and accessed in event-emitter but as in socket, I am passing the value, it is null and thus, not updated on frontend

const eventEmitter = req.app.get('eventEmitter');
eventEmitter.emit('getBookQty', foundBook.qty);  

eventEmitter.on('getBookQty',data =>{
    io.emit('getBook', data);
})


// app.js file
socket.on('getBook', (data) => {       // the program is not getting into this method.
    console.log("from app.js" , data);
    qty.innerHTML = data    
})


Solution 1:[1]

As far as I am able to understand first is EventEmmiter is on the backend side.

So, why do you have getBookQty as an emitting and listening event both on the backend side?

Note: whenever the socket emits any event, by default it emits to all except itself.

So as I'm understanding this in your case when frontend requests for getBookQty it will just execute

eventEmitter.on('getBookQty',data =>{
    io.emit('getBook', data);
})

So, here data variable will be null. It will emit null.

If I am wrong can you share more code separating front-end and back-end?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akshay Bhimani