Arturas
Arturas
Semester Project
Done by:
Artūras Sotničenko
Supervisor:
dr. Joana Katina
Vilnius
2019
Preface
This course work was done during the 6th semester of the study program Information Technologies
in the branch of Innovation in applications. The topic Real-time pixel placement web application
was chosen by me because I wanted to study and apply real-time communication technologies in
web applications.
Artūras Sotničenko
2
Contents
Preface 2
Abstract 4
Santrauka 5
1 Introduction 6
1.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.2 Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Analysis 7
2.1 Web applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Web Application technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Real-time communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4 Related Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.4.1 milliondollarhomepage.com . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4.2 reddit.com/r/place . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3 Requirements 11
3.1 Functional requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Non-functional requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
4 Project Details 12
4.1 System architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1.1 Client tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1.2 Server tier . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.2 Use Cases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.3 Activity diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
5 Implementation 15
5.1 Back-end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5.1.1 Data models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
5.1.2 Data models methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.2 Front-end . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3 UI components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3.1 Pixel Placement component . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3.2 Color Picker component . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3.3 User Management component . . . . . . . . . . . . . . . . . . . . . . . . 19
References 21
3
Abstract
This course work consists of research and implementation details of an interactive real-time
web application where clients can place pixels into HTML5 canvas and receive real-time updates
of such pixels from other user sessions. Course work paper describes the process of development
of such web application with analysis of the field and related work in production, application
system design choices, provides insight into system architecture through diagrams, user interface
drawings, and other UML drawings.
4
Santrauka
Žiniatinklio aplikacija kurioje realiu laiku gaunami ir siunčiami vaizdo elementai
Šis kursis darbas įvardija detales kaip buvo kuriama tinkliažinio aplikacija kurioje klientai gali
delioti pikseliu į HTML5 canvas elementą ir gauti naujausius atnaujinimus realiu laiku. Šis darbas
apibendrina šios aplikacijos kūrimo procesą - atllikta analizė kas yra web aplickaciją, susijusių
darbų analiė, sistemos architektūrinius sprendimus tiek aprašant juos tiek per UML diagramas,
vartotojo sąsajos komponentus bei priešinius.
5
1 Introduction
With the advent of HTML5 and its API from WHATWG that enables developers to building
content-rich and interactive web applications via accessible manner in various devices ranging
from computers, mobile phones, TV, tablets, IoT devices be consumed by web browsers without
any external dependencies or technologies such as Adobe’s Flash, Microsoft’s Silverlight and Java
applets in order to facilitate rich interactions.
There are quite a few HTML5 APIs worth mentioning. This course work will build only on
a small fraction of it. Mosty new EventStream API and Canvas API. In the context of course
work subject, there is a need for real-time updates. Real-Time communication between server
and client browser - using SSE and EventStream would be one of the fits. An alternative for such
communication would be WS API or ajax long pooling. To facilitate the drawing of the pixels onto
users screen Canvas API will be utilized. Similar APIs are often utilized by internet advertising
firms to place advertisements in web sites, browser-based multiplayer games to propagate game
state changes.
The goal of this course work is to develop a working web application where the main feature
would be a facilitation of pixel placement onto canvas element and real-time canvas updates.
1.1 Motivation
The idea of creating a "Real-time pixel placement application" system came up while thinking
about a simple project that I could complete in my spare time from full-time work that could utilize
the skills that I currently possess and skill set that expand upon it. Receiving academic credits and
the possibility of making a spin-off application to monetize it also was taken into account.
1.2 Goals
The main goal of this project is to develop a sweb application that could facilitate interactions
between application users in real time in an easy, understandable and accessible manner. The
application system itself should extensible in terms of features such as user authentication and
additional functionality.
6
2 Analysis
Developing a web application consists of a few important steps. In this section, the analysis of what
is a web application, techniques, and technologies that can be used to develop web applications.
that can be used to develop as well as studies of similar applications will be made.
7
Figure 1. Screenshot of a similar project (milliondollarhomepage.com)
2.4.1 milliondollarhomepage.com
Web site pictured in 1 Is one of the first successful pixel ad places created by Alex Tew. "The
website of Alex Tew, a 21-year-old entrepreneur, who hopes to pay his way through university by
selling 1 million pixels of internet ad space for $1 each." The main idea behind this web site is to
sell batches of pixels with a minimum order of 100 pixels (10 × 10) square and display it for at
least five years. ( cite from about section [8] )
Analysis of source code 2.4.1 shows that the main image is 1000 × 1000 pixel gif format image
that was constructed from 3306 different graphical ads. Each ad has its metadata such as title href
and coordinates. The metadata of the ad is mapped onto image the using HTML <map> element
8
with each of its child element DOMnode <area> containing the attributes with corresponding
metadata values as shown in a code snippet 1
In conclusion, 2.4.1 is an accessible web site that could be used with understandable data
structure, business model and commitment to long term service as it is online since 2005. The
drawback is that quite a few pixel placements leads to 404 pages as the placements are quite old.
2.4.2 reddit.com/r/place
9
The reddit.com/r/place was created as a creative April Fools project to explore the way humans
interact at large scales. [7] In a nutshell the /r/place project is similar to 1 by the canvas size figure
1 figure 2 but the difference was that /r/place allowed collaboration and did not require anything
except users account to place pixel onto canvas. Another difference was that the community could
place pixels only for a brief amount of time as the project lived 72 hours and all the changes
to canvas were propagated in real time to all collaborators. The development, technologies and
thought process behind this project is described in well-written blog post [7]
10
3 Requirements
In this section basic funtional and non-functional requirements for the real-time pixel placement
will be defined
• To inprove UX users should be able to zoom into specific are of the canvas
• From UX stand-point users should be able to see updated pixels of the canvas in real-time
• Users should be able to lock the pixel so no one else could update them for period of time.
• Users should update or edit personal information such email, password, etc.
• Web application should not depend on any other 3rd party plugins or software
• Web application state should always be up-to-date thought other’s users session
• Web application should have REST endpoints that comply with openAPI standarts
11
4 Project Details
4.1 System architecture
There are 2-Tiers that web application must implement from a technology stack perspective -
Client application that contains UI layer with its components responsible for graphical represen-
tation to the end user and communication with the server tier that is responsible for serving static
files to client and handling request form front it, hardware [9]. The system architecture layering is
represented in figure 3
Our end user will use the application in his web browser through our web facing UI. The problem
are that there is many different web browsing software vendors and every one of them has their
quirks which can influence user experience UX. The same follows for different screen sizes, Inter-
net connection speeds and hardware users are using can that make UX different. The standard way
of implementing web application UI is using HTML, CSS, and JS because all of them have fairly
good support in most modern browsers despite minor differences in the the rendering of newer
document properties. One of the solutions for problems mentioned earlier is using HTML, JS and
CSS properties and attributes that are widely supported through different web browsers.
The netwoking between client and server will go through HTTP because its standard support across
different web browsers and easily goes through most network firewalls. For server-side logic
and API endpoints as well as event-stream in for of SSE for application use of LB3 framework
based on [1, Event-driven I/O server-side JavaScript environment node.js] that consistently shows
better performance on server response times compared to alternative Apache web server as were
shown in performance investigation of node.js [6] [3]. The reasoning behind later pick is the use
of the same language for both front-end and back-end so no mental switch is required between
different languages while developing applications while consuming less time and speed up the
system development time. For data store and persistence document based MongoDB document
based database is used for its flexible schema which helps develop applications faster due to the
way, it stores object model and performance [10] Both web application and database is running
inside docker environment as Docker containers communicating through a virtual network and
mounted on host volume for data share.
12
Figure 3. Web applications architecture diagram
13
Figure 4. "Real time pixel placement " Use case diagram
SSE. After the initial load of DOM content, flow branches into two flows - one is responsible for
subscribing HTML5 EventStream Source and listening to change-stream and then updating can-
vas element, while other flow is responsible for displaying the UI and facilitating interactions of
the users pick of color from color picker element and pushing the new pixel with is position to
back-end service. based on [9]
14
Figure 5. Pixel placement activity diagram
5 Implementation
5.1 Back-end
The back-end of the application is built on LoopBack 3.
"The LoopBack framework is a set of Node.js modules that you can use independently or
together to quickly build REST APIs. A LoopBack application interacts with data sources through
the LoopBack model API, available locally within Node.js, remotely over REST, and via native
client APIs for iOS, Android, and HTML5. Using these APIs, apps can query databases, store
data, upload files, send emails, create push notifications, register users, and perform other actions
provided by data sources and services."
15
I picked this framework subjectively as it is simple and powerful enough to provide the func-
tionality for requirements, that I have - server-sent events, CRUD operations, and data persistence
abstracted away.
LB3 provides a way to define data models and connect them to an array of data sources for persis-
tence.
Data in MongoDB has a flexible schema. Collections do not enforce document structure.
This flexibility gives data-modeling choices to match application requirements. As for the initial
data schema only one model code: 2 was attached to data-sources to persist, as there was no
need for user authentication. Models in LB3 usually are defined through CLI tools provided by
LB3. The output JSON shown in code 2 In short line 10 and 15 says that model named Pixel has
two properties: ’l’ (stands for location) and ’c’ (stands for color) both of string attribute and ’a’
(arbitrary property name means lock) that’s is boolean.
Location property attribute is constructed as a strig to reprent coordinates on <canvas/>
element. Exmaple of pixel location attirbute string on canvas would be x : i >= X >= j
y : i >= Y >= j X and Y stands for a integer number in 2D plane between i and j. In the context
of this application, the canvas element is square i and j are between the 0 and square edge length.
16
5.1.2 Data models methods
LB3 framework provides API to add ’Remote methods’ to existing data models. Each remote
method can be invoked in different stages of HTTP request handling. In this course work, I imple-
mented a new remote method called ’patchByLocation’ to facilitate an activity that is described in
fig 5 and is described in code snippet 3. The mentioned method, patchByLocation, takes three ar-
guments shown on code 3 lines 27 to 29, described in subsection 5.1.1. To process the request the
script first queries the model data source with find query by location property (l). Then it checks
whether the model has ’locked’ property set value and branches the response path depending on
the result. The response is provided as a callback method that resolves to either error or updated
Pixel instance as implemented in code 3.
17
5.2 Front-end
User Interface is implemented using the most popular web browser technology stack - HTML5,
CSS, and JS. Upon initial HTTP request, only static files are served (index.html and assets). The
rest of the communication is conducted via an HTTP request to REST API endpoints. The in-
dex.html contains all the markup needed to load the JavaScript files and minimal structure inside
<body/> DOM element for three UI components that will be displayed. [9] [4]
5.3 UI components
The web application is composed of three main components - the Pixel Placement component, the
Color Picker component and the User Management component as per component architeture syle
[9]
Pixel Placement UI component contains Canvas DOM node and functionality to enable Users
interaction with it such as ’Zoom effect’ as the user hovers over the canvas and mouse click han-
dlers which captures the user’s mouse position and sends that information to back-end server after
converting into an acceptable data structure. On initial Web application load Pixel Placement com-
ponent Canvas Element is initialized by filling it with values from Pixel data structure code 2 that
was received upon initial GET HTTP API request to api/Pixels/ endpoint. After that Event listen-
ers are attached to components div element that listens to mouse events such as click, move and
EventSource API that listens to Event-stream coming from the back-end. Each click and mouse
movement over component is handled by invoking its handler.
On the mouse move event over Pixel Placement component, custom Magnifying glass appears
that creates a zoom effect for the area of the mouse pointer is over.
On every mouse click inside the Pixel Placement component a custom click handler is invoked.
The sequence of the mouse click - it gets the mouse position on the canvas relative to users window,
then gets the color value from the Color Picker component and uses Fetch API to send an HTTP
POST to back-end API /Api/P ixels/patchByLocation endpoint with a body containing Pixel
data structure.
Event-stream events originated from back-end are handled by EventSource. EventSource sub-
scribes to event-stream by initializing new EventStream with Event-Stream endpoint
/Api/P ixels/change − stream?_f ormat = event − stream as an argument.
Each EventSource emitted event is handled by custom handlers that parse the data and decides
if and how it needs to update to updated the Canvas element.
Color Picker Component allows User to pick 24bit RBG color with a mouse and place it as a Pixel
with color onto the Pixel Placement component. The component’s source was borrowed from the
Mozilla developer network and modified to fit the needs of the application. The Color Picker
Component provides User Interface to select a 24bit color as well as exposes API to query the
selected color by other components.
18
5.3.3 User Management component
User Managemet component provides UI forms for the user to log in, register and manage it’s
user’s profile when logged in. User Managemet component facilitates communication with back-
end to call Users API endpoints to send and retrieve users data. The User Management component
implements an authentication protocol with an Access Token. The authentication protocol se-
quence to get users infomation from back-end server is simple - User provides his login username
and password submits the form. If the credentials were correct he receives users ID and Access
Token as a response. Then using using users ID and access token another request is made to re-
trieve User Profile information such as password and other details. The Access Token is saved as
cookie to Users browser to persist. Access Token has time-to-live timestamp that describes how
long the Access Token can be used for in order for user to be authenticated.
19
Conclusions and Recommendations
Developing a real-time pixel placment web application was defined by analysis of what is web
appliaction, its technologies and related work. Requirements section was derived from analysis
section. The analysis showed that there are similar web applcations and substatial interest in
such web applications from markets perspective. The requirements were defined similar to those
projects that were found in related projects. The implementation itself followed 2-Tier system
architecture design style that had Client tier that is composed from user interface components that
lives inside the web browser. The Server tier was implemented to comply with the requirements
from system architecture stand point - REST api, with protocols for authentication, model classes
with its attributes and methods. The changes to canvas were propogated in real-time between
Client(s) and servers.
For future references such web application could be made as widget to include in popular
content managment systems, blogs, or web site builder to make sites more interactive as they are
now.
Acronyms
API Application Programming Interface. 12, 17
JS JavaScript. 12
UX User Experience. 12
WS WebSocket. 7
20
References
[1] Ganesh Iyer. Node.js: Event-driven concurrency for web applications. 01 2013.
[2] Salvatore Loreto and Simon Pietro Romano. Real-time communications in the web: Issues,
achievements, and ongoing standardization efforts. Internet Computing, IEEE, 16:68--73, 09
2012.
[3] Brad Peobody. Server-side i/o performance: Node vs. php vs. java vs. go. https://www.
toptal.com/back-end/server-side-io-performance-node-php-java-go, 2017.
[4] L. Shklar and R. Rosen. Web Application Architecture: Principles, Protocols and Practices.
Wiley, 2003.
[5] Vivek Kumar Singh. Long polling vs websockets vs server-sent events. https://medium.
com/system-design-blog/long-polling-vs-websockets-vs-server-sent-events-c43ba96df7c1,
2019.
[6] Vivek Kumar Singh. Node js versus python 3 fastest programs. https:
//benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/node-python3.html,
2019.
21