views:

525

answers:

4

Although iPhone support JSON natively, AMF is a binary protocol and it supposes to use much less bandwidth. Do you think using AMF is a good idea?

Just found this AMF library in cocoa (Objective-C): http://github.com/nesium/cocoa-amf/

Here's the famous benchmark that shows AMF is smaller and faster than JSON + gzip in Flex: http://www.jamesward.com/census/

A: 

You could also try plist, a native binary format. Any format, include AMF or even XML plist, could be reduced by zip. zlib is part of iPhone.

drawnonward
thx, but plist doesn't work well with most web servers though.
Henry
You should add enough details to your question to make your requirements clear. Vague questions get vague answers. Initially, your only reason for using AMF was to reduce bandwidth, in which case plist with zip is a better choice.
drawnonward
I understand, I didn't vote your answer down.
Henry
+2  A: 

I don't think AMF would be significantly smaller than JSON. In fact, it can be slightly larger in many cases. Let me show this in an example:

AMF stores the string "asdf" in the following binary format:

0x12            /* type = string */
0x00 0x04       /* length */
'a' 's' 'd' 'f'
/* total: strlen(s)+3 bytes */

while JSON stores the string "asdf" in strlen(s) + 2 bytes if there are no quotes in the string.

AMF stores the JSON object {"key1":"asdf","key2":"foo"} in the following binary format:

0x03             /* type = object */
0x00 0x04        /* length of key1 */
'k' 'e' 'y' '1'
0x02             /* value type = string */
0x00 0x04        /* length of value1 */
'a' 's' 'd' 'f'
0x00 0x04        /* length of key2 */
'k' 'e' 'y' '2'
0x02             /* type of value2 */
0x00 0x03        /* length of value2 */
'f' 'o' 'o'
0x00 0x00 0x09   /* end of object */
/* total: 30 bytes, while the JSON string is 28 bytes */

The above examples were in AMF0, but I don't think AMF3 would be much different.

The only feature in AMF0 that can significantly reduce the bandwidth is that it contains a reference type: if you send the same large object twice, the second object will be only a back-reference to the first instance. But it is a rare case IMHO (and it works only for objects, not for strings).

So I would recommend JSON (if you really want to spare on bytes, you can compress it with zlib or anything): it's much simpler to read, there are much more implementations, and the specification is clear (while the Flash implementation is sometimes different from the specification - we all like Adobe ;))

gyim
INTERESTING! Thanks.
Henry
but isn't transporting binary over HTTP more efficient somehow than text?
Henry
Not at all! Maybe what you're referring to is that the textual data of a _HTML form_ is URL encoded by the browser (it escapes space character to %20, for example), which increases length. But it is not needed to send data this way over HTTP. And anyway, textual characters are the subset of "binary characters" (numbers between 0-255), so if you send a text as binary data, it cannot cost more than a "real" binary data! (actually, only the contrary can happen: some protocols require that the data is sent in ASCII characters, so the binary data must be unpacked to the 7-bit range...)
gyim
+1  A: 

You might take a look at Hessian or Google protocol buffers if you want to use a binary protocol. I know for a fact hessian provides very good performance on the iPhone.

http://code.google.com/p/protobuf/

http://hessian.caucho.com/

ordord00
+1  A: 

Actually it's a pretty good question and I have it too. I attended a session today at WWDC talking about client/server with iPhone. And they kept telling us binary plist were far more efficient than JSON and XML, especially when it comes to parsing time. But the problem is that I'm still tryin to find any server-side implementation of plist as a remoting protocol, whereas AMF has plenty of great implementations on the server-side: WebORB, ZendAMF, BlazeDS, etc. So integrating AMF on the server side is a breeze. Unfortunately, on the client side, the only option I found was Nesium's Cocoa AMF, but unfortunately it doesn't support channel set authentication and it misses a client-side stub generator. I would look into it but since this is no small task and I'm sure plenty of iPhone developers have already faced that issue, I want to make sure there really are no other options.

Up until now, I've been using Hessian with HessianKit, but it doesn't support authentication either and it's starting to be a limitation. Apple can say all they want about Flash, but at least they make it very easy to connect to a remote server.

Sebastien
no Java implementation of plist yet? maybe it's time to start an open source project. :)
Henry