views:

95

answers:

5

Is it possible to make real-time network games using JavaScript? I've seen flash do it, but I'm interested in making a multiplayer browser-based game that doesn't depend on any plugins. I've read that it is impossible to keep Ajax connections open for streaming communication, and it isn't feasible to make several new Ajax connections per second to keep the client in sync with the server.

A: 

It looks http://socket.io/ is good solution.

thejoshwolfe
A: 

Yes, and it doesn't require any special libraries as certain people seem to imply.

Christian Sciberras
I question whether the author would want to set about this large task without using JavaScript libraries. Libraries and plugins are different. E.g JavaScript libraries can be downloaded at runtime in the page without requiring user action (so not a big deal); whereas plugins must be explicitly installed into the browser by the user.
John K
I question why would someone install Windows (tm) just to have Calculator functionality. You get my point...
Christian Sciberras
I understand calculator usage is overkill for Windows platform; however the author is proposing an opposite scenario: bringing something large that normally requires speed and resources into the "meek" browser platform. So you lost me on the analogy :)
John K
Well, I didn't tell him *not to use* libraries, I'm just saying that I did this kind of stuff before, without any specialized software...it involves a persistent connection (a normal ajax call) and the server delaying the reply for ~25secs (or until a change in situation). Not such a big task. See here: http://stackoverflow.com/questions/3235973/php-code-to-convert-php-to-js (t'was a long story though...)
Christian Sciberras
A: 

It is. Look into a technology called Comet (like Ajax on steroids). Lift (a Scala web framework; twitter and others use it) has excellent Comet support build-in.

alpha123
A: 

As I know sockets without plugins is possible only in HTML5.
But you can use flash to accomplish this task. Since almost every browser supports flash now I think it is ok.
Also there are some hacks which allow do the same thing without bunch of ajax calls. Try to find Long Polling.
Hope it helps

Eldar Djafarov
+1  A: 

WebSockets are the solution for realtime (low latency) networking with JavaScript in the browser. There are fallbacks for providing the WebSocket API with Flash.

You can stick with JavaScript on the server and use something like http://RingoJs.org which has connectors for WebSockets. If you use those two you get at this:

// SERVER
websocket.addWebSocket(context, "/websocket", function(socket) {
  socket.onmessage = function(m) {
      // message m recieved from server
  };
  socket.send('my message to the client');
});

// CLIENT
var ws = new WebSocket("ws://localhost/websocket");
ws.onMessage(function(m) {
   // message m recieved from server
   // do something with it
   return;
});

ws.send('message to server');
oberhamsi