javascriptnetwork-programmingclient-serverp2ppeer

One server that acts as a middleman to connect a pair of clients or server and client for each pair


I am developing a web application where two browsers will need to "connect" to each other in order to play a two-player game.

My initial thoughts on how to go ahead with this were to have one central server, which matches up a pair of clients and acts as a sort of "middleman". When browser A wants to make a move, it sends this to the server. The server then sends it to browser B. From my understanding, this is a typical client-server model.

However, an alternative approach would be to have one browser, e.g browser A that acts as both a server and a client and browser B will only act as a client. When browser A makes a move, it simply sends to brower B, (the client) and vice versa. Every time there is another pair of browsers that connect to play the game, one will act as a server and the other will be just a client.

I thought the second approach could be a peer-to-peer approach, however exactly half the clients will be strictly only be a client so I'm not sure if you can define it as a peer-to-peer model.

So my questions are:

  1. Is there a specific name for the second approach?

  2. What would be the better approach? (given that there isn't much heavy processing and I'm not too worried about scalability as of yet)

I am looking to implement this using javascript and node, and based on that it seems the first approach seems a bit more straightforward, however I cant really justify it apart from that


Solution

  • Browsers cannot host servers. You need to have one central server, as clients cannot connect to each other, and someone needs to serve the webpage itself. Someone needs to port forward, that is the server's job.

    For question one, your method doesn’t exist.

    For question 2, the best approach is to have the server manage everything. However, you can have the server tell one client to do the calculations and simply forward the packets sent between them. There is no best solution, it depends on how powerful your server is and how demanding your game is.

    I would recommend that for a basic game such as connect 4 you have the main server control the actual game, this also prevents cheating. However games like among us with a lot going on will have the server forward stuff between the "host" client and "client" client.