0

I want to make an browser based chess game. Where 2 users take turns making moves on the same board.

At the moment I've set in motion a tomcat server with spring that picks up rest calls made by the node server.

However I'm making a rest call every time any user makes a move. Although the response happens very quickly as there is very little to be done on the server side.

But I feel this may not be the best approach. Is there a better alternative or would rest calls be sufficient in this scenario?

user1111111
  • 403
  • 2
  • 4
  • 13
  • Possible duplicate of [How can I make a browser to browser (peer to peer) connection?](https://stackoverflow.com/questions/7022383/how-can-i-make-a-browser-to-browser-peer-to-peer-connection) – Stephen C Mar 10 '18 at 01:10
  • 1
    If your data is not large, then the absolutely simplest and most reliable way for browserA to send something to browserB is to do it via your server. browserA sends your server a message that asks the server to deliver message X to browserB. The server can then deliver that to browserB. It will be most efficient if both clients are connected to your server with a webSocket or socket.io connection. There are peer to peer technologies, but they have all sorts of potential compatibility issues with firewalls, proxies, etc... Peer to peer technologies are trying to fool those firewalls. – jfriend00 Mar 10 '18 at 01:32
  • A simple client webSocket connection to your server is 100% reliable, simple and trouble-free. As long as you don't have large amounts of data flowing through your server (which you would not for a simple chess game, even if you had hundreds of thousands of simultaneous chess games), this is absolutely the best way. – jfriend00 Mar 10 '18 at 01:34
  • @jfriend00 - This is not the best way if two players in (say) Australia are trying to play using a server in (say) Japan. Network latency can be a killer. (Have you ever played lightning chess?) – Stephen C Mar 10 '18 at 02:31
  • @StephenC - It's a game of chess. A few extra ms of latency in when a move is announced isn't going to be noticed by anyone or relevant to anyone. If this was a real-time interactive multi person shooter game, then the latency might matter. But, for a game of chess, there's just no reason to choose anything but the simplest and most reliable implementation which is the client-to-server-to-client scheme. – jfriend00 Mar 10 '18 at 02:35
  • We are talking hundreds of milliseconds per transit. (Sydney -> Tokyo is ~115ms, Sydney -> Barcelona is ~315ms). The actual delays can add up to seconds due to multiple round-trips, packet losses, etc. It matters enough to be freaking annoying ... if you are playing lightning chess. And obviously the OP cares ... or he wouldn't have asked. – Stephen C Mar 10 '18 at 02:43
  • @StephenC - Do you know the game of chess? A 10 second latency wouldn't even matter most of the time, and certainly a latency of under 1 second which would be more typical would not even be noticed. If the OP wants to specify a required latency, then we can have a more meaningful discussion. This is question of whether taking on the complication and support burden of building and maintaining a peer-to-peer architecture is worth it or not. If you think it is, then please write an answer that explains the architecture and the rationale for why it's worth it. – jfriend00 Mar 10 '18 at 03:02
  • No. The OP is not asking for an architecture for P2P gaming, or a rationale as to why it is worth it. The latter is self-evident. I am simply pointing out to you that a 10 second latency DOES matter if you are playing games like Chess or Go or whatever using a clock. And if you've ever played on a Chess / Go / whatever server, you will know that it is common ... even normal to play with a clock. – Stephen C Mar 10 '18 at 03:31
  • I frequently do it. And one of the reasons that I don't play at "lightning speed" (e.g. in Go, 25 moves in < 2 minutes) is that it is simply not viable unless you are in the same country as the server. If you has experience doing this, then you would understand what I mean. (And yes. I do know the game of chess. Duh! I don't play anymore, but that's not relevant.) – Stephen C Mar 10 '18 at 03:32
  • (By worth it ... I mean worth considering it. In practice, I recognize that implementing P2P gaming is difficult. But if I (as a regular user of a Go server) was offered the experience, I would say that it was worth it.) – Stephen C Mar 10 '18 at 03:40

1 Answers1

1
You can use the websocket.io with nodejs for the real-time communication between users.

If you don't want to use the server at all then you can use the webrtc that uses peer to peer connection.

Here are few useful references:
https://dev.to/rynobax_7/creating-a-multiplayer-game-with-webrtc
https://rynobax.github.io/jump-game
https://github.com/jwagner/webrtc-pong
https://www.webrtc-experiment.com/#featured

For Nodejs and socket.io
https://github.com/fbaiodias/phaser-multiplayer-game

Thanks
Adal Singh
  • 106
  • 2