Thinker
Thinker

Reputation: 14464

Can I improve AJAX with Flash?

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) ?

Upvotes: 1

Views: 267

Answers (5)

Makram Saleh
Makram Saleh

Reputation: 8701

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

Upvotes: 2

Colin Coghill
Colin Coghill

Reputation: 1549

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.

Upvotes: 0

AviD
AviD

Reputation: 13112

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).

Upvotes: 0

James Black
James Black

Reputation: 41858

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

Upvotes: 2

Makram Saleh
Makram Saleh

Reputation: 8701

  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.

Upvotes: 3

Related Questions