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.
6 Answers
Use WebRTC instead of WebSockets for access to peer-to-peer and UDP. See here: Does WebRTC use TCP or UDP? and WebRTC vs Websockets: If WebRTC can do Video, Audio, and Data, why do I need Websockets?

- 1
- 1

- 56
- 3
It looks like http://socket.io/ is good solution.

- 5,328
- 3
- 29
- 21
-
1I used socket.io in [groovebasin](https://github.com/superjoe30/groovebasin) and it definitely works great. Groovebasin is not a game, but socket.io meets all the requirements. – thejoshwolfe May 07 '12 at 22:54
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');

- 1,303
- 8
- 18
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

- 23,327
- 2
- 33
- 27
Yes, and it doesn't require any special libraries as certain people seem to imply.

- 27,509
- 17
- 111
- 155
-
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 Oct 09 '10 at 22:38
-
I question why would someone install Windows (tm) just to have Calculator functionality. You get my point... – Christian Oct 09 '10 at 22:42
-
1I 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 Oct 09 '10 at 22:46
-
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 Oct 09 '10 at 23:01