-1

Hello i am trying to make a multiplayer game with nodejs and socket.io. I am using multi process socket.io with cluster and socket.io-redis. It works well if you want to broadcast messages, emit etc.

But if i want to add some complexity in my code problems start to appear. I want my game to have a matchmaking function.

Assume this scenario: Server find 2 users that want to play and start a game. Users are on different processes on the same machine. The problem is that a client can communicate with only one process the one that firstly got in. So there are 3 possible solutions as I see it:

  1. Matchmake with users that is on the same proccess --- Not good.
  2. Create an ipc method between processes so the one with the target client can broadcast client's answer to the correct process --- Too complex and not sure if solves everything.
  3. Change client's socket.io process to a new one without the user notice it --- Not sure if this is even possible.

Is there something i am missing here? Is there any other solution that i can't think?

Any help appreciated!

Alex Michailidis
  • 4,078
  • 1
  • 16
  • 35

1 Answers1

0

With socket.io-redis users can communicate even if they are in different servers/processes, this is why it exists.

DevAlien
  • 2,456
  • 15
  • 17
  • did you understand my question? – Alex Michailidis Sep 19 '15 at 15:22
  • Apparently not, but it seems you said wrong things in your questions. With socket.io users in same or different processes can communicate. – DevAlien Sep 19 '15 at 15:23
  • i want to listen from process B an event from user which is on process A. – Alex Michailidis Sep 19 '15 at 15:28
  • SocketIO Client A (webpage) sends a message `io.emit('playing', {to: 'user2'}, func...` the server will get that message `socket.on('playing'...` and then send the message (thanks to socket.io-redis) to the other user. What you will want is when they are playing a game you want to make them join a room for that game, so you will emit messages to the room. What about that? – DevAlien Sep 19 '15 at 15:33
  • i dont want to communicate from user to user. I want user A which is in process A to send a message that eventually reach process B and then server decides what to do with this message. – Alex Michailidis Sep 19 '15 at 15:36
  • Yes why not user A and user B (2 different processes) joining one room when they want to start a game and then send a message, the server decides to send it or not. I don't get your point of being in process A or B or C, what is the problem with that? – DevAlien Sep 19 '15 at 15:39
  • i understand what you are missing. if a user is connected in process A you can only access him from process A, when i say access him i mean listen new events from him not sending him messages. So if randomly the game starts on process B i can only bind events from users that are on process B, i lose everything that comes from users that are on process A. – Alex Michailidis Sep 19 '15 at 15:48
  • But you would listen to BOTH because BOTH of them have to join the game. Why do you say the game starts on process A or B, the game should start in BOTH, they should share the resources, as they share the socket.io sessions via socket.io-redis. This is what I don't get. There should be no problem from my point of view. – DevAlien Sep 19 '15 at 15:51
  • i can see what you say. in your implemetation all data are saved in redis, but this add extra complexity. let say we need to calculate overall points for this game, which process will do this? also there should be a locking mechanism in case both process want to write in the same place. I was thinking that a game should be run only in one process so this is process is responsible for everything in this game, now you suggest a game should be split in many processes? – Alex Michailidis Sep 19 '15 at 16:05
  • Saving the data in a DB will help you, what about process A (where the game is run) crashes? You lose your game? In my opinion, if it crashes the client that was connected there will just connect to another one, connect to the room and keep on the fun – DevAlien Sep 19 '15 at 16:09
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90122/discussion-between-alex-rokabilis-and-devalien). – Alex Michailidis Sep 19 '15 at 18:24