What is FlashMOG?

FlashMOG is an open source project to help you create multiplayer online games (MOGs) using Adobe® Flash® by simplifying the Actionscript™ code you must write to make an RPC across a 2-way socket connection. FlashMOG consists of a basic socket server written in PHP and Actionscript classes to put in your Flash project. FlashMOG is intended to jump many of the hurdles faced by novice game programmers while remaining lightweight, fast, and compatible with other frameworks.

Is it complicated?

If you have not written any Actionscript before, you probably won't understand how to use FlashMOG. The most recent versions are for Actionscript 3 and PHP5 and are intended for use by experienced programmers.

How does it work?

Currently, FlashMOG consists of two simple sets of code:

  1. a handful of Actionscript 3 classes for inclusion in your Flash project
  2. a simple PHP project that implements an elementary socket server

How FlashMOG Works Diagram

FlashMOG handles the details of socket communication so you don't have to. Complex data (i.e., an array or object) is automatically bundled for safe transmission across the socket. You can call functions you define on your server directly from your Actionscript code—and vice versa—without having to parse any XML or do other data juggling. You can think about how your game should work rather than fretting about how to get data back and forth.

Developing with FlashMOG is quite similar to using AMFPHP or some other remoting package.

  • Write some Actionscript to define FlashMOGService objects in your Flash project.
  • Attach some event handlers to each FlashMOGService to handle incoming requests from the server.
  • Write PHP classes on the server to handle requests from your Flash client
  • Use RPCs to transfer information and trigger actions

Unlike most remoting services, FlashMOG Server can initiate actions in a FlashMOG Client which are not merely a response to polling or some request from the client. This permits the server to communicate such initiatives such as an incoming chat message or missile attack. Unlike a browser connection which is only open long enough to grab the files, a socket connection stays open for 2-way communication.

A major difference from other PHP projects is that FlashMOG's server must run as daemon. At the moment, this will likely require that you have shell access to your server to fire it up. We are attempting to set up a browser-based admin tool. See the FlashMOG Documentation for more information.

Sample Code

In order to use FlashMOG, you will need to write some Actionscript in your Flash movie and some PHP which you will upload to the server. On the Actionscript side, FlashMOG makes communicating with the server possible with very little code. Here is some sample Actionscript:

/*** ACTIONSCRIPT 3 ***/
import net.flashmog.FlashMOGService;

// first param is the host, second is the port where your server is listening
// the third is serviceName which flashMOG uses to route requests to the right obj.
myServiceObj = new FlashMOGService('mydomain.com', 1234, 'myService');

// define methods here in the client that can be called from the server
// parameters can be number, string, array, or object!
myServiceObj.client.clientMethod1 = function(arg1):void {
    trace('  arg1=' + arg1)
myServiceObj.client.clientMethod2 = function(param1, param2):void {
	trace('myServiceObj.clientMethod2 running')
	trace('  param1=' + param1);
	trace('  param2=' + param2);

// You can also add event listeners like this
function method3Handler(evt:FlashMOGDataEvent):void {
  // data will be found in evt.data and can be
  // an object, an array, string, number, etc.
myServiceObj.addEventListener('clientMethod3', method3Handler);

// the function to run when the Service's socket connects (or fails to connect)
function onConnect (evt:Event):void {
	trace('myServiceObj connection success!');
	// goto the action frame with your awesome game
myServiceObj.addEventListener(Event.CONNECT, onConnect);

// the function to run when the Service's socket disconnects
function onClose (evt:Event):void {
	trace('SERVICE CLOSED!!');
myServiceObj.addEventListener(Event.CLOSE, onClose);

// tell the Service to connect!

The code above imports all the FlashMOG actionscript and defines a FlashMOGService object. Experienced Actionscript programmers should find the coding style very familiar. Once myServiceObj has connected, you can call server functions directly from Actionscript like this:

/*** ACTIONSCRIPT 3 ***/
myService.server.serverMethod1("Yo server look!  The answer to life, the universe, and everything!", 42);

That last line of Actionscript instructs myServiceObj to send a message to your server telling it to locate the an object instance of class myService (or whatever class name we passed to the FlashMOGService constructor) and call its method named 'serverMethod1' with the two parameters we provide. As long as the params you provide to the function call match those expected by the server, you can use any type of parameter including Objects, Arrays, Strings, and Numbers. For those of you familiar with the built-in XMLSocket class, it should be apparent that FlashMOG makes it more convenient to use socket connections between PHP and Flash.

For serverMethod1 to be defined on your server, you must define a PHP class—myService—on your server in the FlashMOG services folder with a method called 'serverMethod1'. It can be quite simple and FlashMOG provides a few data structures and functions you can use to manage connected clients. Here is a sample implementation of myService:


class myService extends FlashMOGService {
    public function 
serverMethod1($textArg$numberArg) {

$arg1 'This string you are reading is $arg1, defined in serverMethod1 of class myService';

// serverMethod1()

public function serverMethod2($arrArg) {
$arg1 'This string is defined in serverMethod2. serverMethod2 calls myService.clientMethod2 with the array passed to the server from Flash.';
// serverMethod2()

// class myService


The particular PHP class above makes use of an inherited class property calling_client which refers to a proxy object representing the client that made the current request to call the current server method. This client object allows you to directly call the Actionscript methods attached to myServiceObj in your Flash project. The server can call a given clientMethod directly on any connected client. In this example, serverMethod1 calls the method 'clientMethod1' on whichever client called serverMethod1 in the first place.

Depending on your coding experience, this introduction is either too little or too much. For more information, see the FlashMOG Documentation.

Last Edited: 2009-09-11 11:58:00 PDT