5.7 Tutorials: Sending Objects

» Introduction

One of the interesting and usefull features in SmartFoxServer is the ability to send and receive complex data structures (arrays, objects, etc.) very easily and transparently. In this tutorial we'll show you how this feature can really speed up the process of creating complex multiuser applications and games.

» Data structures

In the previous tutorial we concentrated our attention on the "User Variables" and we've learned how to use them.
"User Variables" are very usefull when you want to store settings for each client connected and have all the other users receive notices about changes in those values.

There are cases in which this approach is not the best one, for example in a turn-based game.
Classic turn based games can be board games (Chess, Connect 4, ...), role-playing games, card games, strategy games, etc.

Every time a user makes a move in a "Connect 4" game or in a "Chess" game we don't really need to store that move as a "User Variable". We would just need to be able to send the move data to the other clients, without storing it anywhere.
One more example can be a multiuser whiteboard application: anytime someone draws something on it we should just send the information about the lines and curves that were drawn in a convenient format and again no need to save that data.

The sendObject() method of the SmartFoxClient API allows us to do this very easily, check this example:

move = new Object()
move.px = 100
move.py = 200
move.color = "red"

sendObject(move)

The other clients will receive an onObjectReceived event:

smartfox.onObjectReceived = function(obj, user)
{
        // do something here
}

where obj is the object received and "user" is the User object of the sender.

The following list represents the type of variables that can be sent through the sendObject() method:

» object
» array
» number
» string
» boolean
» null

All the other variable types are ignored.

To give you an example that is a little more complex, imagine you're playing an RPG game and you have to send some data
about your character:

myChar = new Object()
myChar.name = "Tarkus"
myChar.position = {px: 100, py:100}
myChar.inventory = new Array("knife", "short sword", "long sword", "hammer", "shield")

sendObject(myChar)

As you can see you can nest other objects and arrays and create pretty complex data structures.

In the next tutorial we'll use this feature to implement a first simple multiplayer game.

» What's behind

It is not the purpose of this tutorial to show how this mechanism works, however we can discuss the basics of it in order to better understand what's going on and how to get the best out of it.

The process of sending and receiving structured Actionscript objects is done through a serialization/deserialization mechanism.

In simple words, an AS object is transformed in a more convenient format (xml in this case) and sent to the server which in turn will transmit the received data to the other users. As soon as the client API receives the serialized data it will re-construct the object and make it available to the application.

It's always important to take care of the amount of data that you're sending and receiving. The more complex and nested is the structure of your objects the more time it will take to send it through the network and if you exceed with informations your application may not respond as expected.

If your application/game is targeted to broadband users then this should not be much of an issue, however slower modem connections have limited bandwidth (4 to 7 KB/s) and you will also have to deal with the inevitable lag of internet connections.

Also if you waste bandwidth by sending unnecessary data you will steal bandwidth to other users connected to the server and if you saturate it the general performance of the clients connected will deteriorate.

The previous RPG game example should not give problems to slower modem connections, however it can be optimized efficiently by
replacing those strings in the inventory array with numeric IDs.

myChar = new Object()
myChar.name = "Tarkus"
myChar.position = {px: 100, py:100}
myChar.inventory = new Array(1, 2, 3, 4, 5)

sendObject(myChar)

1, 2, 3, 4, 5 represent the various inventory items.

You can save about 30 bytes by using this new object: with 100 connected users, you're saving 3 KB/s (24Kb/s) of bandwith; if the users are 1000 you save 240 Kb/s, which is almost half of a DSL connection.

After seeing the theory behind sending and receiving Actionscript data through the SmartFoxServer, in the next tutorial this knowledge will be put to work in a real world example: a complete simple game of Tic-Tac-Toe.


doc index