'Socket io as a service with Express REST API

I have a REST api using express server and I want to receive notifications from certain routes using Socket.io as a service.

Can I emit from withing an express route on a already running express server and have a client listening to my api on it's root to log notifications?

What I'm trying to do:

in my route controller

exports.authenticate = (req, res) => {
... on success:
    socket = io()
socket.emit('logged in', "new user logged in")
}

my socket io client:

let socket = io('localhost:3000', {cors: { origin: '*' }})
socket.on('logged in', () => {
console.log("login notification")
})

I'm running my api alone, and then I'm running my client script using node client. but I'm not receiving anything



Sources

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

Source: Stack Overflow

Solution Source