Hi everyone,
I'm working on a custom Node-RED module using socket.io. I’ve defined a ui_base.js file where I set up a Socket.IO server and handle events. Here’s a snippet of my server-side code:
socket.on('disconnect-request', (targetSocketId) => {
const targetSocket = uiShared.ioServer.sockets.sockets.get(targetSocketId);
if (targetSocket) {
targetSocket.disconnect(true); // Disconnect the target socket
console.log(`Socket ${targetSocketId} has been disconnected`);
} else {
console.log(`Socket ${targetSocketId} not found`);
}
});
socket.on('custom-event', (data) => {
console.log('Received data:', data);
socket.emit('server-response', { message: 'Acknowledged', data });
});
});
I'm trying to emit a custom-event from a Node-RED Function node like this:
const data = { message: 'Hello, server!' };
socket.emit('custom-event', data);
However, I'm not receiving any response back from the server.
I’ve also tried emitting the event inside a socket.on('connect', ...) block, but I still don’t see any progress.
Could anyone suggest how to establish communication between the Function node and the Socket.IO server? Am I missing something in the setup?
Thank you in advance for your help!
List item