views:

185

answers:

5

I got an AJAX online game that calls server few times per second. AJAX calls are good, but still they are slower than normal TCP/IP-sockets connections.

So my questions is, can I improve my game by using - if flash is available - some flash application for server connections? Or maybe there is some solution with Firefox addon (70% of my users uses Firefox) ?

+3  A: 
  1. Create a Flash file that handles your game calls via the built-in XMLSocket objects
  2. Hide that flash in your HTML (width/height = 1)
  3. Use flash.external.ExternalInterface.call from Flash to call JS functions

Flash/JS communication via ExternalInterface is very fast, which can handle the speed provided by sockets replies.

I hope this helps.

Makram Saleh
+1 - for suggesting way to have flash call js to reuse what already exists.
James Black
Problem is, I don't know Flash at all, and I'm looking for already working solution.
Thinker
@Thinker - You are the thinker, so you should could ;) but seriously, this website is not dedicated to beginner questions and so we don't have beginner answers and all the details. Makram's answers seems enlightening, so go ahead!
Isaac
The overall improvement from calling XMLSockets directly will be minimal. The overhead for HTTP is negligible, compared to the overhead of XML, and possible server-side bottlenecks. Also its not good practice for web apps to use anything but HTTP...
AviD
+2  A: 

Before doing this optimization you may want to first profile, to ensure that the slow part is the socket connection between javascript and the server.

I tend to profile the server-side, and then I profile from javascript to the server and back, and the difference is due to the socket connection.

Once you have some numbers, then any other change you make, such as what Makram suggested, can be profiled, to see if there is sufficient improvement.

If your calls to the server are some sort of polling you can look at using Comet to help with that: http://en.wikipedia.org/wiki/Comet_%28programming%29

James Black
A: 

Similar to what James Black said, don't jump straight ahead and rip replace everything to Flash, before you figure out where the bottleneck is (if one exists).

I think you might also be missing some parts of the picture, judging from some of the comments...

I want to lay out all the layers in your communications, just to make things clear - point is, its unlikely ALL of it is the problem, and youd be better looking at the problematic layer only.

  1. Client code - currently javascript, which calls the next layer asynchronously. If you optimize to Flash, this part may be more responsive, and its "richer".
  2. TCP/IP (wont go lower than that) - this is ALWAYS stateful, because TCP is stateful. At least at the connection layer... What this means, is that usually the TCP connection is kept open for a long time, and you dont open a new one on each request. You won't be changing this in a browser app...
  3. HTTP - stateless in principal, but typically circumvented through some form of cookies and server-side session. Also it's not the MOST efficient protocol, especially for binary data, since it is text-based and a bunch of overhead. While it is technically possible to skip through this protocol, its highly UNrecommended to do this in a browser app, because (a) its unexpected from a user point of view, and (b) its not very firewall friendly.
  4. XML - if you discover that your bottleneck is in the amount of data transmitted, you might just want to switch out the payload format since XML is pretty verbose. For example, JSON would be a great alternative here. Or maybe just trim down the XML schema...
  5. Server side app - often, the bottleneck will be here, regardless of anything that happens downstream.

So, to sum up - switching your client to Flash might have 2 possible benefits: the client itself may run faster (depending on your client), and it allows you to call sockets directly bypassing 3 above (http). Again, note that the 2nd benefit is dubious at best - benefit is questionable, and there are clear downsides to it.
Unless the bottleneck is the client display code, you're better off either switching to JSON (or other data format), or optimizing your server code. Once you profile and figure out where the problem is, you'll better know where to focus your efforts. I find it highly unlikely that Flash will help with that. (again, since it IS a game, you may need the improved display).

AviD
All you have written is smart. My game sends frequently small amount of data. 50-250 bytes. HTML overhead is about 200% or more. Server creates a response in 1-2ms, ping to server is 60ms, and AJAX response is about 120ms. So I think stateful connection will help, but I fought, someone already did something like that.
Thinker
Again, as far as communications go, you're already holding the TCP connection open. It does sound like the slowness is resulting from the overhead (you're probably sending several K over the wire each request, when you need much less than that), and probably also the XML parsing routines. My specific recommendation to you is to start by getting rid of the XML, try out JSON or something else. Don't bother ripping out the HTTP, thats not worth the tradeoff (but you can definitely optimize it). Btw, you didnt mention how much data gets returned?
AviD
I use JSON, return value is about 20 bytes. And I see there is a difference between ping server.com -l 50 and ping server.com -l 500 or 1000. So I send 50 or 100 data and get response time like from ping server.com -l 1000
Thinker
Anyway, I'm still looking for already written Flash library to substitute AJAX.
Thinker
I'm a bit confused... you're using JSON *and* AJAX? I don't understand how that would work... Anyway, if it IS the communications thats slowing you down, your choice of client technology will have zero effect.
AviD
A: 

If you do this, please make sure you do something sensible when the user has a proxy configured, even if it's just a message telling the user that proxies aren't supported.

Proxies are so easy to forget.

Colin Coghill
+2  A: 

Just happened to come across this new JS library that might be helpful for you (announced today by ajaxian):

Kamaloka

From their site:

Kamaloka-js is an implementation of the AMQP messaging protocol in native JavaScript. It is setup to be used with Orbited but can be used with any library which produce TCPSockets in the browser similar to Orbited.


Another similar solution (using flash): amqp-js

Makram Saleh
Thanks for the bounty!
Makram Saleh
That was exactly what I was looking for :)
Thinker