The intent of this message is to provide a little clarification to those wanting to write their own Appcelerator backend (Service Broker). Appcelerator Service (server side) and Message (client side) Brokers use a simple, standard JSON protocol to send and respond to messages. Data is posted to the server via an Ajax request and the Service Broker first parses the request to find messages, which are mapped to some service handler, function, or method via the type (name) of the message. Next the Service Broker delegates the message to the handler to finish its computation and returns the result using the same protocol definition. More detail on the protocol definition is available here, but the basic structure goes something like this:
{ "timestamp": 1214008045871, "version": "1.0", "messages": [ { "type": "app.test.message.request", "scope": "appcelerator", "version": "1.0", "data": {"message": "Hello,world"} } ] }
where messages is an array of messages. Each message is defined as follows:
{ "type": "app.test.message.request", "scope": "appcelerator", "version": "1.0", "data": {"message": "Hello,world"} }
where type is the name of the message, scope describes which elements on the client will receive the message, and data is a JSON object containing the data payload of the message.
Astute readers might have gathered that the request/response procedure is synchronous. This decision was made after trying both polling and synchronous methods. Synchronous behavior was chosen for its efficiency and because it did not adversely impact the user experience on the client.
One final note: It’s important to set the content type in the http response to “application/json” so the client knows how to parse the incoming messages.
Popularity: 2% [?]
