Patryk Cieszkowski
Patryk Cieszkowski

Reputation: 751

Socket.IO server + Socket.IO-client

Im actually desperate now. I need to design and code solution I've never even thought I'd be making, and it seems completely unrealistic to achieve (for me) with clean express. But I might be wrong.

What I need is:

But now, what I need is to some efficient way of making it, to communicate with each other. So the app has to be basically a bridge between all of them. I was thinking of using some kind of state managing library, like Redux solution, that way I (at least I think) could achieve it pretty easy. But I also haven't found any of this kind, for the node, or neither found how to achieve it on the backend.

So the question is: what's the best solution, to achieve one-to-many and many-to-one communication between multiple socket.io connections? Again, to make it clear.

socket-client connection = connecting to already existing server, as a node app.

I am not speaking of making simple solution for clients to communicate between each other on 1 server. I want to create 1 server, and to connect to multiple others.

Upvotes: 0

Views: 505

Answers (1)

MildlySerious
MildlySerious

Reputation: 9180

Okay, so, what you probably want to do is to create one "server" application, then connect your Electron app as well as the bots as clients to that server.

For the bots, which would be running as their own node scripts, you would be using the socket.io-client package, and then use that the same way you use the client in the browser. Call io() with your connection settings, get a socket from that, and start binding your events as usual:

let io = require('socket.io-client');
let socket = io('http://YOUR_SERVER_IP/');

socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});

If you want to tell the difference between the bots and the Electron clients, I would send out some special event on connect, that your server can then handle:

socket.on('connect', function(){
    socket.emit('identify_as_bot', {id: IDENTIFIER})
});

I hope that helps.

Upvotes: 1

Related Questions