2. The abstract of suggested solution defines a II. OVERALL FRONT-END ARCHITECTURE AND STRATEGY
communication protocol built on JSON-RPC (JavaScript The front-end framework project (figure 1) is part of the
Object Notation - Remote Procedure Call) with well-defined overall architectural redesign of Tuenti.com. The front-end
scopes of responsibilities for technological components of layer is responsible for rendering views, communication with
the client and a highly customizable structure of server-side the server and UI interaction. The second part of the project
controllers. includes back-end redesign which is outside of the scope of
Several frameworks exist that introduce similar solutions this document. Main concerns identified by the stakeholders
in order to decouple HTML from data and perform rendering for the system and that are addressed by the front-end design
at the client browser. include: flexibility, cost and schedule, and integrability.
ExtJS [1] allows placing templates in separate files that The frontend framework's principles are to produce a
can be fetched and cached by the browser. Later, AJAX is highly decoupled system by introducing a natural separation
used to fetch data from the server to populate the templates. of concerns in the source code: structure (HTML), layout
This leads to bandwidth savings as there is no redundant styles (CSS), behavior and control (Javascript), and data
HTML served in every pageview and cost of producing (JSON-RPC). All except for the data will be cached on the
rendered HTML is moved to the client browser. However, client and in content caching solutions reducing the load of
the drawback of Ext JS approach is that they are introducing the in-house infrastructure. Furthermore, due to the nature of
a new language in the templates and therefore increase the some requests that return mostly unchanged data, it is
cost of any UI and design related operations. Consequently possible to cache and reuse them. This opportunity is
there is an increased need for interaction between designers supported by dramatically (45% to 91% according to the
and client-side developers. Also, the HTML templates tests) reducing the size of the response produced by the
produced by designers must be converted into Ext Templates server.
increasing the possible points of failure and the complexity The decoupling mentioned above also supports the
of the development process, as well as making maintenance organizational aspects of development projects based on the
cumbersome. framework. Work can easily be parallelized between teams
Many other JavaScript libraries essentially work with a participating in the project. The only required interaction
similar concept. These include: Mjt [2], UIZE [3], TrimPath between them takes place in the analysis stage, when the
[4] or EmbeddedJS [5]. An implementation worth interface and the model are defined to satisfy product
mentioning here is one done by PureJS [6], which tackles requirements. Projects that involve redesign of the user
this issue by creating HTML templates that do not require interface can remove (or minimize) the need for developer
usage of conditional or looping statements. The templates involvement due to lack of any transformations to the
remain in HTML and the data is matched with injection templates that are now based on pure HTML.
points identified by a specific attribute in the structure. JSON was picked as a data transport format because it is
Conditional statements are triggered by simply hiding or technology independent (easily parsed) but also native for
showing an element. In order to implement loops, they are the presentation layer of the main client, which is written in
inferred automatically by detecting if the data is an array. Javascript. It can be easily (in accordance to PCA design of
However, PureJS does not effectively manage decoupling of the server-side) replaced by XML or other formats on
data from the structure. More complex (real) usages of demand. The communication protocol itself has been
PureJS framework require constructing statements named designed to address the need for semantic identification of
"directives". These define the process of inserting the data the data, human readability, and server-side optimizations.
into HTML along with other elements such as additional data
structures or adding user interaction.
The existing system produces server-side Views as part
of the MVC framework. In a multi-client environment this
leads to an added load on the server infrastructure along with
increased complexity of implementing any changes to the
presentation. Currently the system uses seven different
interfaces, each with a separate set of views, controllers and
dynamic templates. The new solution will reduce that to
much simpler and static templates. An additional limitation
of the current MVC system is that it is capable of producing
only one view per URL with a request; in contrast, a request
with a chained JSON-RPC call can perform several
operations and return data that can be used for display,
caching, or configuration of the client. The new approach
also opens several optimization opportunities in terms of
reusing bootstrapped instance and in-memory cache of the
system and number of client connections.
33
3. configuration of the client such as re-routing the client to a
different farm, throttling automatic chat status updates, etc.
Example JSON-RPC call:
{
"Friends": ["getOnline",
{"maxCount": "10", "order": "recentVisit"}],
"Messages": ["getThread",
{"id": "12670096710000000066", "msgCount": "5"}],
}
The server will provide a response in JSON which has a flat
structure, so it does not infer or suggest any hierarchy on the
f i n a l d i s p l a y, a n d i s p u r e l y d a t a - c e n t r i c .
{"output" : [
{
"friends" : [
The performance concern is being addressed by the
{
possibility to chain multiple calls in one request; this
technique not only reduces the need to bootstrap the system "userId" : 234,
but also can drastically reduce the number of connections "avatar" : {
between the client and the server (which is especially "offsetX": 31,
significant for mobile applications). "offsetY": 0,
See figure 2 for a more detailed view of the execution "url": "v1m222apwer124SaaE.jpg",
flow. },
"friendAlias" : "Nick"
III. CLIENT-SIDE FRAMEWORK DESIGN },{
The approach followed in the framework makes use of a "userId" : 638,
similar concept followed by Pure JS for templates. However, "avatar" : {
all behaviour and logic is provided by client-side JavaScript "offsetX": 32,
only and the mapping between the data and template "offsetY": 50,
structure is performed automatically basing on the "url": "MLuv22apwer114SaaE.jpg",
semantical information contained in both. Since the },
mentioned mapping information is contained within standard "friendAlias" : "John"
id and class attributes of the HTML tags, they can be }]
naturally reused by Javascript and CSS without introducing }, {
any new meta-language. "threadId": "12670096710000000066",
Effectively, the framework resides on the user browser "canReply": true,
and is responsible for interacting with servers to fetch static "totalMessageNumber": 3,
and dynamic data, template rendering, and user interaction. "messages": [
Specifically, the server interaction involves sending requests {
for downloading all statics from the content caches by "isUnread": true,
pooling and parallelizing requests for optimal bandwidth
"senderId": 32,
usage and total time of handling the request. Upon receiving
the response from the servers the framework executes the "senderFullName": "John",
hooks for client data transformations (e.g. date "body": "But I must explain to you...",
transformation) and renders the page. The Main Controller is "validBody": true
the core of the client-side framework, as it manages the }, {
communication with the servers and handles several hooks "isUnread": false,
allowing code execution within the process of handling user "senderId": 66,
action. "senderFullName": "Paul",
The data is retrieved by sending JSON-RPC requests to "body": "Sed ut perspiciatis unde omnis...",
the server which provide the response to called procedures, "validBody": true
but also can contain additional information. This will usually }]
contain data corresponding to the actions that took place }]
within the functional scope of interest of the user (e.g. new }
message has arrived) but also can contain data used for
34
4. The page structure is defined in pure HTML. This means response buffer that later is printed (currently only JSON
that the templates themselves do not require introducing any printer) and output from the system.
new meta-language, relying instead on the framework to The Abstraction layer plays a relatively small role in the
show/repeat pieces depending on the interpretation of the PCA structure and only interacts with the Domain layer. But
response data. It is possible though to extend execution of its presence is very important from the perspective of the
the user action by arbitrary routines that can add additional back-end framework.
logic. Still, that doesn't influence the way that the templates See figure 3 for a general model.
are being created and all of the routines for handling None of the PCA agents produce the presentation as
templates are implemented in Javascript. such. Instead the response is sent to a Response Buffer where
Here is an example of a piece of HTML code which each agent caches the data it produces. The Response Printer
serves as a template to display user avatars: is a lazy component which just produces a representation of
the data when the full request has been performed (when
<img params="userId url offsetX offsetY" Printer asks for it). The Response component allows greater
class="avatar" src="" alt="" control over re-usability of responses across the hierarchy in
onclick="Tuenti.Modules.Avatar.onClickEvent(this);"/> a request, allowing reuse of the agent response throughout
the lifetime of a request. At the end of processing, the Printer
With this code sample the Template Engine is capable of component can accept any printing strategy to produce
implicitly performing a very flexible conditional statement output in desired format.
where it is possible to show the element basing on the The complexity of each agent is dramatically reduced as
presence of the avatar in the data, repeat it if the avatar is an the framework does not produce any views. The Abstraction
array, and inject data into the DOM element basing on the layer of an agent will access the Domain layer, which is part
information contained in the params attribute. of the backend framework, to fetch the requested data. The
If no matching with the data can be found the DOM Controller contains all the actions an agent can perform and
element will be left unprocessed. The data centric approach it may instantiate multiple Abstraction objects to fetch data
of the framework means that the Template Engine will to build its output. The structure of controllers provides them
identify elements in the structure by iterating through the with a lot of flexibility. A Controller can delegate tasks to
data, and matching them in the DOM structure that is being other agents or fetch their responses and then perform the
dynamically scoped. When iterating into nested structures, requested action. Additionally, the controllers are capable of
the Template Engine will now search only within the accessing responses of other agents through the response
corresponding context in the DOM. If certain DOM elements buffer.
are not identified by the Template Engine they are left
unprocessed and their default appearance will apply. In order
to match elements to data the DOM elements only need to
refer to the data through values set in two distinct element
attributes, class attribute for data to be injected in the page
structure and params attribute for data to be made available
to UI interaction scripts.
Specific actions for user interaction can be added to the
DOM elements by specifying the action. All actions will be
implemented in an external static JavaScript file and, as a
good practice and internal coding convention, it will not be
allowed to place any other JavaScript code inside the HTML
Templates. This is a natural decoupling of behavior from
structure which is similar to decoupling page structure from
style by not setting inline CSS using the style attribute.
IV. SERVER-SIDE FRAMEWORK
The main architectural element of the server-side part of
the front-end framework is inspired by an architectural
pattern known as Presentation-Abstraction-Controller (PAC)
[7][8]. The design (named Printer-Controller-Abstraction)
bases on identifying data-centric controllers and allows free
interactions between them. The graph structure of controllers
receives data from the abstraction layer where abstractions
are instantiated by controllers. The abstraction layer
communicates with the domain layer that manages and
identifies domain entities. All of the controllers that
participate in the request processing populate a central
35
5. V. CONCLUSION AND FUTURE WORK This successful PoC shows that highly complex and
The new design improves the project organization feature rich applications can be developed with the proposed
performance by reducing inter-team dependencies and framework. Server-side code complexity is greatly reduced
abbreviating communication to its initial analysis stage. through the usage of the PCA framework. Preliminary results
Apart from that it reduces amount of work that is required to show that response time is almost a third when compared to
prepare the front-end code for the rich UI clients. Teams are the existing MVC framework. Templates can now be
able to focus more on their core activities and technologies visualized directly in the browser, raw template sizes are at
reducing friction and optimizing the communication paths. least 30% smaller, and there are no conditional or iterative
Upcoming visual redesign projects can be done primarly by a flows.
design team rather involving significant work from client- Cost of producing rendered HTML is now moved to the
side developers to support iterative changes to views, client browser's which might now become a challenge that
templates and controllers. Simple and pure HTML templates has to be faced before rolling out the system into live
improve the throughput of designers who may now work in a environment. However, the overall response time observed
WYSIWYG way, able to use their tools directly on the by the user is still lower than in the current implementation
templates. and will be subject to further optimizations.
Key benefits of this architecture are:
REFERENCES
minimize overhead of maintaining multiple
interfaces, [1] Ext JS. Palo Alto CA (USA), 2009 [Online]. Available:
www.extjs.com
shift team focus to match their responsibilities,
[2] Mjt, "Template-Driven Web Applications in JavaScript" [Online].
removing the need of server-side changes when Available: www.mjtemplate.org
changing page structure, layout, design or UI scripts, [3] UIZE, "JavaScript Framework", 2010 [Online]. Available:
parallelize development project efforts, www.uize.com
minimize bandwidth consumption (savings of 45%- [4] TrimPath, 2008 [Online]. Available: www.trimpath.com
92% depending on the page type), [5] Jupiter Consulting, "EmbeddedJS, An Open Source JavaScript
minimize server-side CPU use (savings of 65%-73% Template Library", Libertyville IL (USA), 2010 [Online]. Available:
www.embeddedjs.com
depending on the page type),
[6] BeeBole, "PureJS, Templating Tool to generate HTML from JSON
improve developer's performance by providing tools data", 2010 [Online]. Available: www.beebole.com/pure
with clearly defined responsibilities and scope. [7] Coutaz, Joëlle, "PAC: an Implementation Model for Dialog Design",
1987. H-J. Bullinger, B. Shackel (ed.). Proceedings of the Interact'87
A working prototype has been built, proving the above conference, September 1-4, 1987, Stuttgart, Germany. North-Holland.
concepts are viable and functional. A subset of an existing pp. 431–436
feature, the Private Messages module, of the Tuenti.com [8] J. Cai, R. Kapila and G. Pal, "HMVC: The HMVC: The layered
application was used to test the above framework. This initial pattern for developing strong client tiers", 2000, JavaWorld [Online].
A v a i l a b l e : http://www.javaworld.com/javaworld/jw-07-2000/jw-
proof of concept (PoC) of the framework has preliminary 0721-hmvc.html?page=2
been evaluated using Firefox 3.5. and Chromium.
36