0

I'm trying to create a real-time multiplayer web game and right now I'm using a database to hold the client actions and each client sends an AJAX to the database to continuously check for the other players action every 100 or 200 milliseconds, and I was wondering if there was a better way to go about this.

Is there a way for the server to actually contact the client if something in the database changes? Then if one client made an action the server could contact the other client to tell it that an action was made. Or is there another way to go about this better than a constant loop that checks the database for each client?

Cains
  • 883
  • 2
  • 13
  • 23

2 Answers2

1

There are multiple HTML5 WebSocket solutions (that push and send data to an implementation, basically) out that that have become increasingly popular for this sort of thing. Pusher is one option (and my favorite) but there are others you can easily find by searching. I'd also give a look at What are Websockets? and this question.

Community
  • 1
  • 1
mattsven
  • 22,305
  • 11
  • 68
  • 104
1

I would suggest you look into a client-server (websocket) framework such as socket.io or SignalR.

With such a framework you can maintain a persistent connection to/from the server. You can combine this with a pub-sub model for raising/passing events to other clients in the same game (after server-side validation).

If you want to log to a database, that is fine, but would not use the database (unless you're binding to redis+pub-sub as an exception) as your transport.

See:

  • websockets
  • comet (long-polling, everscroll, iframe events)
  • pub-sub
Tracker1
  • 19,103
  • 12
  • 80
  • 106
  • Any tutorials or information you could link to? I looked up socket.io and node.js before and even got them installed, but there was so little documentation that I could find on them. – Cains May 28 '13 at 23:32
  • Do a search for `socket.io getting started` and you'll find a lot of basic tutorials... beyond this, I found that Rob Connery's try it quiet presentation was awesome https://vimeo.com/43548699. – Tracker1 May 28 '13 at 23:41