Using Socket.io in the Electron client to communicate with a separate Node server


#1

Hi everybody! This is a project structure of a software I’m developing now. There’s the Node server client which is an HTML page with buttons(public/index.html). When a user clicks the buttons the client sends messages to the server through Socket.io and I want the server to send the same message(which I have done using socket.emit on respective events) it received from the client to another Electron client(in the client folder) through Socket.io again. So I have setup code in the Electron client to listen at port 9090 using socket.on(‘event’, callback) and the console in the Electron app window shows it’s connected to the Node server but the emits from the Node server on button-clicks are not appearing in the console in the Electron app window. Can anybody tell me why’s that happening?


#2

Given what I can understand of your description, things will fail to show up in the console in the Electron app window for one or more of the following reasons:

  1. When the user clicks a button, the button doesn’t generate the initial message
  2. The message doesn’t get sent from the initial client
  3. The message doesn’t make it to the server
  4. The message doesn’t get received by the server
  5. The server doesn’t send the message to the Electron client
  6. The message doesn’t make it to the Electron client
  7. The Electron client doesn’t receive the message
  8. The Electron client doesn’t display the message

Unfortunately, which of the above is going wrong is impossible to know given only your description. If you can narrow down the problem, it will be more likely that someone can give helpful suggestions.


#3

Hi! So, when a user clicks a button in the HTML interface, a message is sent to the Node server which is logged on receiving and thus points 1,2,3 and 4 can be ruled out. The Electron client says it’s connected to the Node server but it’s not certain which one of points 5, 6, 7, or 8 is happening that’s not setting up the communication. The client side code I’m using looks like
var socket = require('socket.io-client').connect('http://localhost:9090', { reconnect: true }); socket.on('connect', function() { console.log('Connected to server.'); socket.on('event', function(data) { //code }) }); The Node server is programmed to emit messages to the Electron client whenever it receives a message from the HTML client. And the code is like
socket.on('move', function (data) { console.log(data); if(data.direction == 'F') { socket.emit('toNBClient', { direction: 'F' }); } else if(data.direction == 'L') { socket.emit('toNBClient', { direction: 'L' }); } else if(data.direction == 'R') { socket.emit('toNBClient', { direction: 'R' }); } });
The communication I’m wanting to set up can happen if I use Socket.io right? or should I use something like this?


#4

I have no idea really, I’ve never used socket.io. I was just using basic troubleshooting to break down the problem :grinning: