please.js is a Request/Response based wrapper around the PostMessage API that makes use of jQuery Promises. Here’s a quick example to load an iframe window’s location:
please.js is based on top of jQuery and the jQuery Promise API. jQuery version 1.6 or above is preferred. To make the communication between two windows on different domains work, both of them must be injected with the same version of jQuery and please.js.
Currently, please.js is an alpha release (0.1.0). Down the line, we would like to add features like support for communication in Chrome Extensions and improving the documentation to make it easier for all users to get started easily.
How it works
The underlying concept is simple. Two frames need to communicate with each other asynchronously. To access one of the child frames on a page, the parent frame sends a please.Request to the child frame. The Request object is a lot like the request sent by the browser to a server. It contains information on what needs to be done in the child frame (call a function, get/set a property or a variable, or access a DOM node using jQuery). The child frame sends a please.Response back to the parent frame with the result of what the parent frame asked. For a function call request, it is the return value of that function, and for a get request, the value of the variable/property is returned.
If you would like to contribute, you can submit an issue on GitHub. Will be great if accompanied by a failing test case and/or a pull request.
In this post, I’ll go through the process of how I created the page using HTML5 and JS. Let’s get started!
Setting up things
I searched about some existing parallax scrolling JS scripts and came across Skrollr.js which made my work a piece of cake! If you are going to create your own parallax scrolling page, then I would recommend you to use this library. Apart from that, I also used scrollTo.js and mousewheel.js for scroll handling.
Also, I wanted to make the images used in that page look sharp on retina screens so I used a little LESS mixin from RetinaJS to make sure that retina screens show the images @2x.
After looking at some examples of Skrollr, I was ready to start building up the page. The best thing about Skrollr is that it automatically set things up for you and also handles the parallax scrolling on mobile devices.
Now, I saved two versions (1x and 2x, for retina) of all the images and searched for a good comic font. Each slide on that page is a mixture of some text and image elements. I gave each slide an absolute positioning and 100% width and height. Also, each element in the slides are fixed positioned are made to appear and disappear using the opacity property. Here’s the code for the first slide:
The only thing that Skrollr needs is the data-px attribute with some CSS properties passed in that attribute. Here, Bob will be at 0% left having 0 opacity at the start. Now if the user scrolls to 1000px, s/he would see Bob’s image appearing from left to the center with increasing opacity. Thats how it works, you just need to time your animations in terms of pixels and Skrollr will handle it for you. Here, both bob and text are fixed positioned. To make things responsive, I first positioned everything to center using this:
After this, I altered the margins to position it perfectly so that on any resolution it will start from the center. I did the same thing for all the elements in each slide. Most of the elements are animated using CSS3 transforms while others are just faded in and out using opacity property.
All this completed 80% of the page. Now, the only thing left was the scroll handling. I had to make sure that on each scroll, a slide should finish the animation properly and should not be left in between. To do this, I created checkpoints of the scroll position where each slide starts/ends. Now on each scroll, I incremented/decremented a counter based on the scroll direction. Based on that counter’s current value the page is scrolled to the position from the checkpoints array and any other scroll event is ignored in that duration. Here’s the code for this:
I also added keyboard navigation, and put some arrows on the page for easier navigation. Also, after getting reviews from some non-technical people, I added the auto-play option so that all the lazy people would still be able to watch the whole presentation without moving a finger :P
This almost completed the whole page. Last additions were creating a preloader for the page which loaded the images of first 5 slides with a progress bar and then rest of the images are loaded in the background. If you want, you can take a look at the preloader.js to see how I did the preloading. Another thing was the share buttons and showing the count which was retrieved using PHP.
I hope this covered everything but if you get stuck anywhere, then feel free to add your comments! :)
Visual Website Optimizer’s editor component loads a website for editing using a
proxy tunnel. It put a big restriction on what kind of websites could be loaded
in it. Websites behind a firewall, the ones on a local network, or behind HTTP
authentication could not be loaded using the tunnel. Other than those, even if
the website did load in the editor, chances were that it could break on the
You’d ask: why is there a proxy in the first place? Because, if a page contains
an iframe on another domain, it cannot access its properties or functions. It is
a security feature that browser vendors offer users to protect their privacy.
So, our task at the frontend recently was to eliminate this troublesome
middleman and find a solution for cross-domain iframe communication. We knew
what the answer was: the PostMessage API.
Provided a customer had VWO tracking
code integrated on their website, we could load the iframe directly without a
proxy and communicate with it using this API. The bigger question, however, was
how to do it. The Editor had a lot of parent-child communication going under the
hood for every task the user performed. When attempting to use PostMessage for
this communication, we were faced with a couple of issues:
Our legacy code had direct communication between the parent frame and the
child frame at all places, i.e. the objects and functions in the child were
accessed synchronously. PostMessage API, on the other hand, is completely
asynchronous, and implementing such an API on the existing codebase would almost
mean rethinking the entire logic and program flow all over again. We could
foresee this asynchronous transition become a cause of a lot of race conditions
within the Editor.
Often, after sending a message to the other frame, we wanted to hear back a
reply, for which we needed a decent two-way communication. A kind that would
keep track of the sender and the receiver and could be identified across iframes
using a unique identifier (to tie up the requests and responses).
Since PostMessage uses string messages for communication (or structurally
objects in the recent browsers), it put a big limit on what kind of
data we could send during this communication. Directly accessing DOM nodes and
sending around certain cyclic objects was no longer possible.
For instance, when you select an element in the child frame, it creates a new
VWO.Element instance in the parent frame and asks it to open a context menu. The
code looked something like this:
While, it might seem like a trivial problem to solve on the cover, deep
underneath, we were faced with a race condition. The Element.create method
asked the child frame to add a class to that element, and the
ContextMenu.showForElement expected the class to have been applied by the time
it was executed.
We concluded that refactoring the code to adapt to the asynchrony would be one
hell of a task and we had to find another way. We decided to write a wrapper
around the PostMessage API to solve the above three problems. We called it
please.js. We are currently giving it some finishing touches before we push it
out to the community. Here’s how we did it:
We decided to build this library on top of jQuery Deferred API. While
deferred objects and promises don’t exactly eliminate the asynchrony, they
somehow bridge the gap between the two, making asynchronous code feel more
linear and flattened. So, using that base, any piece of code that expected code
prior to it to have been executed fully, could now be made possible without
giving a lot of thought. In the above example, the transition to please.js
looked like this:
Although this seems hackish at the first glance, it was a way to rapidly
iterate over synchronous code and convert it to use promises and callbacks
without giving much thought on the logic.
To establish a good two-way communication, we thought of thinking of each
communication as a pair of messages: a request and a response. Under the hood,
we identified each message using a timestamp it was initiated on, and created a
request object with that identifier. We then send the request to the other
frame, whilst storing it in the current frame in a hashmap. The other frame
would then receive the request, perform an appropriate action and send back a
response. After a response is received, the request would be deleted from the
hashmap. To make things easier for us, we created a set of functions to make
certain frequent tasks easier. For instance, getting / setting a property and
calling a function were the most common tasks we performed. The code for these
tasks now looked like this:
A paradigm shift, yet the logic remained unaffected. Exactly what we wanted.
The last task was a big one. We had a lot of code in the parent frame
directly accessing the child frame’s DOM. While this is not advocated as a good
practice, such problems are often faced when building upon and improving legacy
code. With PostMessage, you can no longer access the child’s DOM in any way.
But we came up with a smart solution. We know that jQuery is a wrapper around
the traditional DOM. We created a PostMessage wrapper around jQuery itself!
Which makes impossible turn possible:
This was something that I thought of during one of the hackathons
we host at Wingify. Turned out to be very fruitful!
In my personal opinion, I believe using promises for such a large transition
has greatly impacted the way I think about frontend web development. It is a
way forward for rapid asynchronous development, and yet having a flattened
synchronous-like code structure.
please.js will be opensourced soon, so keep an eye out on the blog for updates!
I clearly remember the summer of 2010 when we were about to launch our
product Visual Website Optimizer out
of beta and almost all the conversations I and Paras had were either around acquiring our
initial customers or about the ever increasing load on our single Linode 512MB VPS. Three years down, we still end up
discussing about the same things but at a completely different magnitude.
The customer base has increased to 2600+ accounts across 75+ countries and
our geo distributed architecture
on a set of 30+ servers currently serve close to 8,000 requests per second.
In this amazing journey what we also managed to do is try our hands on a
bunch of different technologies, tools and libraries. Many a times, the
available stuff didn’t fit our scaling needs and we had to craft our own
versions. With the medium of this blog (which was long due),
my team will try to talk about all the learning we keep
having in our day-to-day engineering work at Wingify.
In the next few posts, we will walk though the evolution in our
architecture (from standard LAMP to a delicately configured openresty CDN & DA (Content distribution & data acquisition
network) environment; from a single data center stack to a distributed
system across the globe), code rewrites / refactors we had to do, various
benchmarks we relied on and of course all the learning we had from this
exercise. We will showcase and write about the small / big tools and
libraries we wrote to scratch our own itch.
If you ever have any question, suggestion, feedback or you want to discuss
anything or just want to drop a hello; I would love to hear from you. Please feel
free to get in touch with me [email protected]
or with our engineering team [email protected] directly or via comments section in this blog.
Hope to see you again soon when we post our first engineering article on this blog.
Please subscribe the blog to stay updated. We’re out to change the way software is written,
so come along and share our journey!