Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've recently started learning JavaScript and HTML and developed simple 2-player game such as tick-tack-toe, battleship, and dots & boxes. However, these 2P games can only be played on one computer (i.e. the 2 players must sit together).

I want them to be able to play with a friend on a different computer.

Would anyone have a general, simple working example of a web-game server?

share|improve this question
The problem with web-based games is that you won't just have two simultaneous players on your server - you will have to handle an arbitrary number of players and assign them to game sessions of two players each. – Philipp Jun 26 '13 at 9:42
@Philipp Maybe he really wants only two friends (i.e. people who decide to play simultaneously) to play together, no matching of arbitrary number of players. – sm4 Jun 26 '13 at 10:07
up vote 6 down vote accepted

In just HTML5 and Javascript, it is almost entirely possible to accomplish this without any backend code (eg. PHP, Python, Ruby) thanks to some of the latest technologies that have come out. Using WebRTC, a peer to peer multiplayer experience is entirely possible, with your server only standing as a broker. To simplify things, there is a library for javascript called PeerJS that abstracts away from the lower level details of WebRTC and make your life much easier. The general idea is you have a Node.js server that will be the broker in establishing a connection between the two clients and once the clients are connected, they will only use peer to peer to communicate after that. Remember that you will need some way for each of the clients to verify the other clients moves are legal, since clients can and will be modified. But if this is just for your own learning experience, that shouldn't be too much of a problem.

share|improve this answer

Web browsers can't act as a server, so when you want networked multiplayer, you will need a central gameserver on the internet to handle the game state. This can not be done with pure HTML + Javascript. You will need server-sided programming for this. There is a myriad of different technologies to implement this. PHP, Ruby on Rails, JSP, ASP.NET, Node.js to just name a few. Which one to use is too subjective for the scope of this website and thus off-topic according to the FAQ.

Then you need to choose a way for the HTML+Javascript client to communicate with the server. Your options are:

  • The oldschool variant with static pages. When a player makes a move, they get redirected to a new page (generated on the server) showing the new game-state. They need to refresh it to check if the other player made a move.
  • AJAX (very well-supported by web browsers, but cumbersome when it comes to waiting for a move from the other player)
  • WebSockets (faster and more suitable for waiting for events, but not as well-supported)
share|improve this answer
"Web browsers can't act as a server" While that's almost always true, here's to completeness: There are freak cases depending on how strictly you define "web server". – Anko Jul 19 '13 at 12:03

You don't actually need a central server. He/She could instead set it up so that the server is created on the host machine. This is not recommended though, as this is slightly insecure if not handled properly. (essentially you are sending the server code that you WOULD be running on the central server to the client. The only difference is that for a mainstream game, a main server would have to handle many players, while sending a server to be set up on the host would only need to support x players, where x is the maximum number)

You should read this:

share|improve this answer
Not just insecure but probably hard to achieve as well. For this to work, you need to have at least one PC with public IP address or some hackaround with VPNs. – sm4 Jun 27 '13 at 7:10
You would have to have ports already opened. Like I said, not recommended, but possible. If I were designing a game just for me and my friend, and wanted it to be pretty private with low overhead, I would probably do it this way. – Jeff B Jun 27 '13 at 12:24
-1 This question is about JavaScript: Clients cannot communicate with each other, except through the server. – Anko Jul 19 '13 at 12:08

You can add multiplayer player support in your games in many ways. Firstly there is a WebRTC(Real-Time-Communication), where you can communicate peer to peer, but WebRTC is a work in progress project and only supported by new builds of Chrome and FireFox.

Then there is a second way by having a central server, through which the devices exchange information. In HTML5 you can use websockets at client side to send and receive messages. On server side, you can use any language like PHP, Node.Js, Ruby, etc and handle connection to those websockets. I won't recommend you to use AJAX as it will not give you a good real time experience. To host your game you can use any PaaS(Platform As A Service) service or even maintain your own server and host it there. But hosting your server involves a great cost and also need proper maintains.

There is also another way to achieve multiplayer game development. It's by using BaaS(Backend As A Service), where you don't need to buy a server. You don't even need to write server side code. These BaaS providers have SDKs for different platforms, where you can use multiplayer service by using their APIs. You only need to write multiplayer logic at client side only.

There are different options available, you should explore all of them, and according to your requirement, skill and resources you can should choose one.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.