<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Trackets Blog</title>
  <subtitle>Simple JavaScript Error Tracking</subtitle>
  <id>http://blog.trackets.com/</id>
  <link href="http://blog.trackets.com/"/>
  <link href="http://blog.trackets.com/feed.xml" rel="self"/>
  <updated>2014-09-29T13:11:00Z</updated>
  <author>
    <name>Blog Author</name>
  </author>
  <entry>
    <title>Tips for building a Single Page Application</title>
    <link rel="alternate" href="http://blog.trackets.com/2014/09/29/tips-for-building-a-single-page-application.html"/>
    <id>http://blog.trackets.com/2014/09/29/tips-for-building-a-single-page-application.html</id>
    <published>2014-09-29T13:11:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;Everyone&amp;rsquo;s building single page applications (SPA) these days. We&amp;rsquo;ll take a
look at some of the challenges and best practices for building JavaScript heavy
applications. Some of the things might not be so obvious at first glance, but
it definitely helps to think about this up front. After all, it&amp;rsquo;s quite a big
decision to go JavaScript all the way.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;

&lt;h2&gt;Don&amp;rsquo;t break the browser&lt;/h2&gt;

&lt;p&gt;While most of the modern JavaScript frameworks out there can handle URLs pretty
well these days, it is still important to sit back and think about this for a
moment. Your application might have multistep forms, which aren&amp;rsquo;t persisted in
the URL, but the user might still want to reload the page without losing
everything.&lt;/p&gt;

&lt;p&gt;There is usually a lot of state on each page of a Single Page Application. It
might be pagination, or the amount of scrolling the user has done when using
infinite scroll, or the detail of something that appeared in a lightbox when he
clicked on it. Such things usually lead the user to another page when using a
regular website, but in a Single Page Application, it usually doesn&amp;rsquo;t. Even
though the user would still expect the state to somehow be perserved.&lt;/p&gt;

&lt;h2&gt;Test on real mobile devices&lt;/h2&gt;

&lt;p&gt;Everyone has at least one smartphone these days, or a tablet, or a small laptop
with a touch screen. The point being, no matter how small your audience is,
they will try to use your application on their mobile devices. I&amp;rsquo;m saying &lt;em&gt;try
to use&lt;/em&gt;, because if you haven&amp;rsquo;t thought about mobile in the beginning, there&amp;rsquo;s
a fair chance that the app won&amp;rsquo;t work very well. The reason why this is a
problem more for Single Page Applications and not for the old-fashioned static
websites, is that the more JavaScript you have, the more likely you&amp;rsquo;re going to
run into something funky.&lt;/p&gt;

&lt;p&gt;The mobile browsers are pretty good at displaying &lt;code&gt;&amp;lt;a href=&amp;quot;...&amp;quot;&amp;gt;&lt;/code&gt; links, but
they might not be so perfect at drawing your fancy charts, or at handling that
parallax scrolling you just added using 5 different hacks. In fact, scrolling
is probably the most absused thing I&amp;rsquo;m running into. It just always feels that
I&amp;rsquo;m either using a mouse on what was designed for a touch screen, or the other
way around. Just because you&amp;rsquo;re developing on a MacBook Air with a Trackpad, it
doesn&amp;rsquo;t mean that everyone in the world uses a Trackpad as well.&lt;/p&gt;

&lt;p&gt;An important thing to note here is that &lt;em&gt;testing on a real device&lt;/em&gt; is not the
same as &lt;em&gt;resizing your desktop browser window to 480x360&lt;/em&gt;. There is a
completely different feel from using your fingers to move around.&lt;/p&gt;

&lt;h2&gt;SEO&lt;/h2&gt;

&lt;p&gt;If your application is facing the public internet, it should be indexable by
the search engines. Imagine if something like Amazon suddenly decided to
rewrite everything using JavaScript. It doesn&amp;rsquo;t even need to be a Single Page
Application, all it takes is that the main content is generated by JavaScript.&lt;/p&gt;

&lt;p&gt;A great example of this is Discourse, which while being a Single Page
Application written completely in Ember.js, still manages to work without
JavaScript at least to some extent. This is not only good for the search
engines, who can easily index the whole forum, but also for users on old mobile
devices, or just bad browsers in general. In fact, I know quite a few people
who have JavaScript disabled by default, and only enable it for the sites they
trust.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;m not really talking about progressive enhancement, since that would take a
lot of effort on your side to make the application work completely without
JavaScript after you&amp;rsquo;ve decided to write the whole thing in Angular. What I&amp;rsquo;m
saying is that it is a good idea to have the core functionality working, at
least to some extent, without JavaScript. This might mean that the users can
read the content, but can&amp;rsquo;t do anything else on the site.&lt;/p&gt;

&lt;p&gt;SPA users have higher expectations Once you start with the whole SPA thing,
your users&amp;rsquo; expectations will grow massively. Take Discourse for example. If
you&amp;rsquo;re replying on a regular forum, you simply hit the reply button and start
writing your reply. If something pops up in your mind and you decide to search
the forum some more, you simply open a new tab in your browser.&lt;/p&gt;

&lt;p&gt;On the other hand, Discourse implemented this neat feature when you can keep
navigating the site, while having your reply still open at the bottom of the
page (&lt;a href="http://try.discourse.org/t/can-i-find-stuff-while-writing-replies/55"&gt;see this
topic&lt;/a&gt;).
This however, while being an awesome feature, brings up a lot of newborn
complexity. For example, they&amp;rsquo;ve also implemented a feature, that if you finish
writing your reply while being on a different topic than you were originally,
you will be prompted to reply to that topic instead.&lt;/p&gt;

&lt;p&gt;&lt;img alt="screenshot" src="http://i.imgur.com/ugJdpEI.png" /&gt;&lt;/p&gt;

&lt;p&gt;You can also collapse (minimize) the reply form, it saves the text as you type
to the backend, etc. Just a bunch of very cool things from a UX perspective.&lt;/p&gt;

&lt;p&gt;But there&amp;rsquo;s a downside to this as always. Think about how much work goes into
designing the workflow I just described, vs creating a simple and static HTML
form. It isn&amp;rsquo;t even on the same scale. You can deliver a much better experience
to the user, but you&amp;rsquo;re also going to delay the whole project by a significant
amount of time. Ever new feature will yield 10 new opportunities for making the
UI better (and more complicated), and in the world of client side applications,
that also means it will take more time.&lt;/p&gt;

&lt;p&gt;Another simple example here might be file uploads. If your website has a
regular file upload, the visitor can simply drag &amp;amp; drop a file to the input and
press submit. If it is a JavaScript application, you&amp;rsquo;re probably going to use
the HTML5 File API to get to the file, then maybe add a nice overlay to show
that the area responds to the user dragging a file, then maybe display a
progress bar of the upload, which will require some backend work, then you&amp;rsquo;ll
decide to add some animations to make it look good. Sure the JavaScript version
might be cooler, but it will also take you a week to do properly.&lt;/p&gt;

&lt;h2&gt;Do you really need a Single Page Application?&lt;/h2&gt;

&lt;p&gt;Before you actually get into building one, really think about what parts of
your application need to be highly interactive. It might be the case that there
is only a single page in the whole app that would benefit from being all
JavaScript, while the rest can easily be just plain old HTML. If this is the
case, you might realize that there is no need for a client side router, since
all the logic is on a single page.&lt;/p&gt;

&lt;p&gt;By limiting the amount of pages that are pure JavaScript you can focus all of
your effort into making those that are really great. There is really no point
in wasting half of your development time on making the about/help/docs/pricing
pages, when you could be working on your core application instead.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Unit Testing JavaScript with QUnit</title>
    <link rel="alternate" href="http://blog.trackets.com/2014/09/24/unit-testing-javascript-with-qunit.html"/>
    <id>http://blog.trackets.com/2014/09/24/unit-testing-javascript-with-qunit.html</id>
    <published>2014-09-24T13:11:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;There are many testing frameworks available these days, most of
which offer some very cool features, such as automated cross browser
testing, etc. But today we&amp;rsquo;ll take a look at something much simpler,
QUnit, which is our all time favorite at Trackets.&lt;/p&gt;

&lt;p&gt;Why? Having 50 different testing frameworks to choose from actually
isn&amp;rsquo;t so good, since you&amp;rsquo;ll be constantly arguing with everyone else
about which framework is the best one, and what is the right way to test
things. There are also many ways to run the unit tests, many different
build frameworks, and just a huge number of choices in general when it
comes down to creating a new JS project.&lt;/p&gt;

&lt;p&gt;The reason why QUnit is our favorite is because you can skip all of the
boilerplate around tools like Grunt. All you need is a web browser and a
single .html file, which will serve as a test runner. You can even use a
CDN for the QUnit source files and use a service like JSBin to get up an
running in zero time. &lt;a href="http://jsbin.com/molebusugowu/1/edit"&gt;Here&amp;rsquo;s an example&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt;QUnit Example&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"//code.jquery.com/qunit/qunit-1.15.0.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"qunit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"qunit-fixture"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"//code.jquery.com/qunit/qunit-1.15.0.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
    &lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Passed!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;To run the tests, all you need to do is just reload the page. Want to
debug the tests? Just insert a &lt;code&gt;debugger&lt;/code&gt; keyword to trigger a
breakpoint and reload the page. There are zero dependencies, zero setup
time, and the tests run instantly.&lt;/p&gt;

&lt;p&gt;QUnit basics QUnit is a very simple library with an easy to remember
API, as there are only a few functions you&amp;rsquo;ll be using.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;module&lt;/strong&gt; - Defines a new module, which essentially groups a bunch of test cases together.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;test&lt;/strong&gt; - Creates a new unit test.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;asyncTest&lt;/strong&gt; - Creates a new asynchronous unit test.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;expect&lt;/strong&gt; - Sets the number of expected assertions in an asynchronous test.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;start&lt;/strong&gt; - Resumes the execution of an asynchronous test runner, we&amp;rsquo;ll talk about this later in this article.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let&amp;rsquo;s examine our hello world example to see how to write a simple test
using the QUnit API.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Passed!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The &lt;code&gt;test&lt;/code&gt; function takes two arguments, the name of the test and the
actual test itself in the form of an anonymous function. The name can be
anything you&amp;rsquo;d like, and the function should accept a single argument,
which is the &lt;code&gt;assert&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;In our example we called the &lt;code&gt;assert.ok&lt;/code&gt; function, which simply asserts
that something is &lt;code&gt;true&lt;/code&gt;. There is also &lt;code&gt;assert.equal&lt;/code&gt; for comparing
values (more assertions &lt;a href="http://api.qunitjs.com/category/assert/"&gt;are documented in the API
documentation&lt;/a&gt;, which means we
could&amp;rsquo;ve written the same example as &lt;code&gt;assert.equal(1, &amp;quot;1&amp;quot;, &amp;quot;Passed!&amp;quot;);&lt;/code&gt;.
All assertions take an optional argument, which is a short description
of the assertion. If you use one of the provided assert functions, such
as &lt;code&gt;assert.equal&lt;/code&gt;, QUnit will show a nice comparison if the test fails.&lt;/p&gt;

&lt;p&gt;The QUnit runner allows you to re-run just a single spec by clicking the
&amp;ldquo;Rerun&amp;rdquo; link, which will allow you to iterate more quickly in case
something goes wrong, since you will be able to focus just on that one
failing test.&lt;/p&gt;

&lt;h2&gt;Testing asynchronous code&lt;/h2&gt;

&lt;p&gt;Since JavaScript is by its nature asynchronous, you should be able to
test such code. This could be either because of AJAX, some animation
running, or just a simple &lt;code&gt;setTimeout&lt;/code&gt; call. We&amp;rsquo;ll use the last one to
see how to test asynchronous code.&lt;/p&gt;

&lt;p&gt;Here&amp;rsquo;s a function that will wait 200 milliseconds and then increase a
counter and call a callback.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You might be tempted to test it by simply attaching a callback with an
assertion.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;But this won&amp;rsquo;t work as expected and will result in an error. Expected at
least one assertion, but none were run - call expect(0) to accept zero
assertions.&lt;/p&gt;

&lt;p&gt;The reason for this error is that the test runner will exit immediately
after the function is called, since the 200ms delay is asynchronous. The
fact that we&amp;rsquo;ve attached a callback doesn&amp;rsquo;t do anything here, since
QUnit has no way of knowing that we actually expect the callback to get
called.&lt;/p&gt;

&lt;p&gt;To combat this issue, QUnit provides a way to stop the test runner, and
resume it later. This is done using the obviously named &lt;code&gt;start()&lt;/code&gt; and
&lt;code&gt;stop()&lt;/code&gt; functions.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The call to &lt;code&gt;stop()&lt;/code&gt; increases a counter, and the call to &lt;code&gt;start()&lt;/code&gt;
decreases it. If the counter is more than 0 when the test function
returns, the QUnit runner will keep waiting until the counter goes back
to 0. The reason for this is that you might have multiple asynchronous
operations that you&amp;rsquo;re waiting for, and multiple assertions. You can
even tell QUnit how many assertions are you expecting using the
&lt;code&gt;expect()&lt;/code&gt; function. Let&amp;rsquo;s see an example.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"hello test"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;We&amp;rsquo;re setting up two times, one that fires after 100ms, and second one
that fires after 200ms. Initially we say that we&amp;rsquo;re expecting 2
assertions, which means that if we mess up our start/stop calls or the
timing is wrong, the test will still fail if only one assertion gets
called. We also call &lt;code&gt;stop()&lt;/code&gt; twice, because we&amp;rsquo;re going to wait for two
separate asynchronous operations.&lt;/p&gt;

&lt;p&gt;The rule of thumb is that there should be the same number of &lt;code&gt;stop()&lt;/code&gt;
calls as there is &lt;code&gt;start()&lt;/code&gt;. The only exception is when you use
&lt;code&gt;asyncTest&lt;/code&gt; instead of &lt;code&gt;test&lt;/code&gt;, which starts out with the counter already
set to one. The following two tests are thus equivalent.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"foo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;stop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;asyncTest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;start&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;While QUnit might not be the shiniest of all the testing frameworks, it
works perfectly and requires almost no setup. You can simply open up a
JSbin and start prototyping your code while writing the tests, without
having to install Node.js, configure Grunt, or do any of those other
unproductive setup things.&lt;/p&gt;

&lt;p&gt;In the next article we&amp;rsquo;ll take a look at how to run QUnit from the
command line using Phantom.js, and how to create a cross-browser testing
setup with QUnit and Karma.js.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>SSH Tunnel - Local and Remote Port Forwarding Explained With Examples</title>
    <link rel="alternate" href="http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html"/>
    <id>http://blog.trackets.com/2014/05/17/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html</id>
    <published>2014-05-17T17:57:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;There are two ways to create an SSH tunnel, local and remote port
forwarding (there&amp;rsquo;s also dynamic forwarding, but we won&amp;rsquo;t cover that
here). The best way to understand these is by an example, let&amp;rsquo;s start
with local port forwarding.&lt;/p&gt;

&lt;p&gt;Imagine you&amp;rsquo;re on a private network which doesn&amp;rsquo;t allow connections to a
specific server. Let&amp;rsquo;s say you&amp;rsquo;re at work and imgur.com is being
blocked. To get around this we can create a tunnel through a server
which isn&amp;rsquo;t on our network and thus can access Imgur.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -L 9000:imgur.com:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The key here is &lt;code&gt;-L&lt;/code&gt; which says we&amp;rsquo;re doing local port forwarding. Then
it says we&amp;rsquo;re forwarding our local port &lt;code&gt;9000&lt;/code&gt; to &lt;code&gt;imgur.com:80&lt;/code&gt;,
which is the default port for HTTP. Now open your browser and go to
&lt;a href="http://localhost:9000"&gt;http://localhost:9000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The awesome thing about SSH tunnels is that they are encrypted. Nobody
is going to see what sites you&amp;rsquo;re visiting, they&amp;rsquo;ll only see an SSH
connection to your server.&lt;/p&gt;

&lt;h2&gt;Connecting to a database behind a firewall&lt;/h2&gt;

&lt;p&gt;Another good example is if you need to access a port on your server
which can only be accessed from &lt;code&gt;localhost&lt;/code&gt; and not remotely.&lt;/p&gt;

&lt;p&gt;An example here is when you need to connect to a database console, which
only allows local connection for security reasons. Let&amp;rsquo;s say you&amp;rsquo;re
running PostgreSQL on your server, which by default listens on the port
&lt;code&gt;5432&lt;/code&gt;.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -L 9000:localhost:5432 user@example.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The part that changed here is the &lt;code&gt;localhost:5432&lt;/code&gt;, which says to
forward connections from your local port &lt;code&gt;9000&lt;/code&gt; to &lt;code&gt;localhost:5432&lt;/code&gt; on
your server. Now we can simply connect to our database.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ psql -h localhost -p 9000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Now let&amp;rsquo;s stop here for a little bit an explain what is actually going
on. In the first example the &lt;code&gt;9000:imgur.com:80&lt;/code&gt; is actually saying
&lt;code&gt;forward my local port 9000 to imgur.com at port 80&lt;/code&gt;. You can imagine
SSH on your server actually making a connection (a tunnel) between those
two ports, one on your local machine, and one on the target destination.&lt;/p&gt;

&lt;p&gt;If we however say something like &lt;code&gt;9000:localhost:5432&lt;/code&gt;, it means
&lt;code&gt;localhost&lt;/code&gt; from the server&amp;rsquo;s perspective, not localhost on your
machine. This means &lt;code&gt;forward my local port 9000 to port 5432 on
the server&lt;/code&gt;, because when you&amp;rsquo;re on the server, &lt;code&gt;localhost&lt;/code&gt; means the
server itself.&lt;/p&gt;

&lt;p&gt;This might be a bit confusing, but it is important to understand what
the syntax actually means here.&lt;/p&gt;

&lt;h2&gt;Remote port forwarding&lt;/h2&gt;

&lt;p&gt;Now comes the second part of this tutorial, which is remote port
forwarding. This is again best to explain with an example.&lt;/p&gt;

&lt;p&gt;Say that you&amp;rsquo;re developing a Rails application on your local machine,
and you&amp;rsquo;d like to show it to a friend. Unfortunately your ISP didn&amp;rsquo;t
provide you with a public IP address, so it&amp;rsquo;s not possible to connect
to your machine directly via the internet.&lt;/p&gt;

&lt;p&gt;Sometimes this can be solved by configuring NAT (Network Address
Translation) on your router, but this doesn&amp;rsquo;t always work, and it
requires you to change the configuration on your router, which isn&amp;rsquo;t
always desirable. This solution also doesn&amp;rsquo;t work when you don&amp;rsquo;t have
admin access on your network.&lt;/p&gt;

&lt;p&gt;To fix this problem you need to have another computer, which is publicly
accessible and have SSH access to it. It can be any server on the
internet, as long as you can connect to it. We&amp;rsquo;ll tell SSH to make a
tunnel that opens up a new port on the server, and connects it to a
local port on your machine.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -R 9000:localhost:3000 user@example.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The syntax here is very similar to local port forwarding, with a
single change of &lt;code&gt;-L&lt;/code&gt; for &lt;code&gt;-R&lt;/code&gt;. But as with local port forwarding, the
syntax remains the same.&lt;/p&gt;

&lt;p&gt;First you need to specify the port on which th remote server will
listen, which in this case is &lt;code&gt;9000&lt;/code&gt;, and next follows &lt;code&gt;localhost&lt;/code&gt; for
your local machine, and the local port, which in this case is &lt;code&gt;3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is one more thing you need to do to enable this. SSH doesn&amp;rsquo;t by
default allow remote hosts to forwarded ports. To enable this open
&lt;code&gt;/etc/ssh/sshd_config&lt;/code&gt; and add the following line somewhere in that
config file.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GatewayPorts yes
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Make sure you add it only once!&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo vim /etc/ssh/sshd_config
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;And restart SSH&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo service ssh restart
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;After this you should be able to connect to the server remotely, even
from your local machine. The way this would work is that you would
first create an SSH tunnel that forwards traffic from the server on
port &lt;code&gt;9000&lt;/code&gt; to your local machine on port &lt;code&gt;3000&lt;/code&gt;. This means that if
you connect to the server on port &lt;code&gt;9000&lt;/code&gt; from your local machine,
you&amp;rsquo;ll actually make a request to your machine through the SSH tunnel.&lt;/p&gt;

&lt;h2&gt;A few closing tips&lt;/h2&gt;

&lt;p&gt;You might have noticed that every time we create a tunnel you also SSH
into the server and get a shell. This isn&amp;rsquo;t usually necessary, as you&amp;rsquo;re
just trying to create a tunnel. To avoid this we can run SSH with the
&lt;code&gt;-nNT&lt;/code&gt; flags, such as the following, which will cause SSH to not
allocate a tty and only do the port forwarding.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ ssh -nNT -L 9000:imgur.com:80 user@example.com
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;SSH has a huge number of features, so I&amp;rsquo;d recommend you to checkout the
manual page at &lt;code&gt;man ssh&lt;/code&gt;, which contains even more tips.&lt;/p&gt;

&lt;p&gt;There&amp;rsquo;s also an amazing talk called &lt;a href="http://vimeo.com/54505525"&gt;The Black Magic of SSH / SSH Can Do
That?&lt;/a&gt;, which I really recommend you to
watch.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Why should you care about JavaScript errors?</title>
    <link rel="alternate" href="http://blog.trackets.com/2014/05/04/why-should-you-care-about-javascript-errors.html"/>
    <id>http://blog.trackets.com/2014/05/04/why-should-you-care-about-javascript-errors.html</id>
    <published>2014-05-04T14:33:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;Most of the websites on the internet aren&amp;rsquo;t just static pages anymore.
Half of the internet is using jQuery, and with the growing popularity of
single page applications we have even more JavaScript in our apps.&lt;/p&gt;

&lt;p&gt;This however leads to completely different problems which aren&amp;rsquo;t present
in standard server-only architecture. When something goes wrong on your
backend, it&amp;rsquo;s usually just a single request that failed and you have all
the context available.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem with JavaScript is that the applications usually live for
more than just a single request.&lt;/strong&gt; One thing might silently fail and
cause an error a few minutes later in a completely different part of the
UI, as the user continues on using your application.&lt;/p&gt;

&lt;h2&gt;Problems unique to JavaScript applications&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Errors on the backend can break the frontend in unexpected ways.&lt;/li&gt;
&lt;li&gt;Some users might click on everything twice, causing race conditions.&lt;/li&gt;
&lt;li&gt;Internet connections are sometimes flaky.&lt;/li&gt;
&lt;li&gt;Requests which usually takes 150ms might suddenly take 15 seconds.&lt;/li&gt;
&lt;li&gt;Users will have your application open for hours without a page-reload.&lt;/li&gt;
&lt;li&gt;Client side data might get corrupted and break seemingly unrelated
parts of your application.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;img alt="screenshot" src="http://i.imgur.com/107OTrl.png" /&gt;&lt;/p&gt;

&lt;p&gt;This screenshot is from a top100 site on the internet.&lt;/p&gt;

&lt;p&gt;It just took a few page reloads and oops there&amp;rsquo;s an error. It doesn&amp;rsquo;t
seem to impact the page at first sight, but who knows. Maybe it&amp;rsquo;s
causing some backend service to not function properly, maybe some
analytical data isn&amp;rsquo;t being collected, or maybe it will cause another
error after using the site for a while. The interesting bit here is that
the error doesn&amp;rsquo;t happen all the time, which means it might go
overlooked when the application is being tested, but then it starts
happening to actual customers.&lt;/p&gt;

&lt;h2&gt;Your application has these problems too&lt;/h2&gt;

&lt;p&gt;Now if a huge site like this has these problems, your application
probably has them as well, you&amp;rsquo;re just not aware of it. Here&amp;rsquo;s where
Trackets will help you.&lt;/p&gt;

&lt;p&gt;You&amp;rsquo;ll see all runtime errors with detailed information about the
context in which they occur.  If a user clicked on a button which caused
the error to happen, you&amp;rsquo;ll see it in the error message.  Without any
overhead or impact on your application.&lt;/p&gt;

&lt;p&gt;You might be thinking that there is already a lot of exception trackers
that are able to handle these kind of problems. The problem with a lot
of them is that they weren&amp;rsquo;t designed to solve JavaScript errors in
particular. &lt;strong&gt;Most of the existing solutions just support
JavaScript as yet another platform among many&lt;/strong&gt;. This approach
works for backend languages, where there is no user interaction, but the
frontend needs more.&lt;/p&gt;

&lt;p&gt;Every client session is different and every user interacts with your
application in a different way. Some users might double-click on a
button where they should click just once. This will work for regular
links, but not single page applications full of AJAX calls, where it
might result in a race condition that breaks the app&amp;rsquo;s functionality.&lt;/p&gt;

&lt;p&gt;All of these problems are just a small fraction of what can go wrong in
your application.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>Giving a meaning to stacktraces</title>
    <link rel="alternate" href="http://blog.trackets.com/2013/11/25/giving-a-meaning-to-stacktraces.html"/>
    <id>http://blog.trackets.com/2013/11/25/giving-a-meaning-to-stacktraces.html</id>
    <published>2013-11-25T15:33:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;We’ve all been there, something goes wrong, you look at a stacktrace and have
absolutely no idea what the hell is going on. If only there was something that
would give you more context, for example a list of all the actions a user had
taken before the error occurred.&lt;/p&gt;

&lt;p&gt;In order to make the whole bug-hunting process less painful we’ve decided to
record all events that lead up to unhandled exceptions. When a new error pops
up, you can just look at the event log and instantly see what happened. Maybe
the user clicked a button 4 times in a row instead of just once. That sort of
thing would never appear in a stacktrace, but it might as well cause a race
condition in your app.&lt;/p&gt;

&lt;p&gt;Or maybe you’re doing AJAX and forgot to handle the error case in some obscure
use case. Then the request fails and your application fails as well, but in a
different place which depends on the request being successful. With Trackets
we’ll not only show you the user events (such as mouse click events), but
network requests as well.&lt;/p&gt;

&lt;p&gt;But this isn’t the end of our story. We still have more features to uncover,
logging events in the browser is just the beginning.&lt;/p&gt;
</content>
  </entry>
  <entry>
    <title>4 Tips for Working with Dates in PostgreSQL</title>
    <link rel="alternate" href="http://blog.trackets.com/2013/08/26/4-tips-for-working-with-dates-in-postgresql.html"/>
    <id>http://blog.trackets.com/2013/08/26/4-tips-for-working-with-dates-in-postgresql.html</id>
    <published>2013-08-26T11:47:00Z</published>
    <updated>2017-11-12T13:02:58+01:00</updated>
    <author>
      <name>Article Author</name>
    </author>
    <content type="html">&lt;p&gt;Those of us who come from Rails aren&amp;rsquo;t surprised when we see something like
&lt;code&gt;5.weeks.from_now&lt;/code&gt; or &lt;code&gt;3.days.ago + 2.hours&lt;/code&gt;, which makes working with dates
much easier. But PostgreSQL got your back on this as well, you can just use the
builtin functions and get most of the same functionality.&lt;/p&gt;

&lt;h3&gt;Current Time/Date/Timestamp&lt;/h3&gt;

&lt;p&gt;There are many ways of getting a current time, but first we need to distinguish
between two types&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;always returns current value (&lt;code&gt;clock_timestamp()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;always returns current value, unless in a transaction, in which case it returns the value from the beginning of the transaction (&lt;code&gt;now()&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let&amp;rsquo;s take a look at an example&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# BEGIN;
postgres=# SELECT now();
              now
-------------------------------
 2013-08-26 12:17:43.182331+02

postgres=# SELECT now();
              now
-------------------------------
 2013-08-26 12:17:43.182331+02

postgres=# SELECT clock_timestamp();
        clock_timestamp
-------------------------------
 2013-08-26 12:17:50.698413+02

postgres=# SELECT clock_timestamp();
        clock_timestamp
-------------------------------
 2013-08-26 12:17:51.123905+02
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As you can see, &lt;code&gt;clock_timestamp()&lt;/code&gt; changes every time the statement is
executed, but &lt;code&gt;now()&lt;/code&gt; always returns the same value. It&amp;rsquo;s also worth noting
that both of these functions take timezone into account.&lt;/p&gt;

&lt;h3&gt;Time interval, aka &lt;code&gt;3.days.ago&lt;/code&gt;&lt;/h3&gt;

&lt;p&gt;You can easily create time intervals using the &lt;code&gt;interval&lt;/code&gt; operator, for example&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;interval &amp;#39;1 day&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;interval &amp;#39;5 days&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;interval &amp;#39;5 days&amp;#39; + interval &amp;#39;3 hours&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;interval &amp;#39;5 days 3 hours&amp;#39;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, we can do simple math using the interval operator, which makes
it very easy to construct things like &lt;code&gt;3.days.ago&lt;/code&gt; just by doing the following&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# SELECT now() - interval '3 days';
           ?column?
-------------------------------
 2013-08-23 12:23:40.069717+02
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h3&gt;Extracting the day of the week and more&lt;/h3&gt;

&lt;p&gt;Sometimes you just want to know the day of the week for a given date, or the
century, or just the day. PostgreSQL has an &lt;code&gt;extract()&lt;/code&gt; function which does
just this.&lt;/p&gt;

&lt;p&gt;Just to put this into context the examples were executed on Monday, August 26.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# SELECT extract(DAY FROM now());
 date_part
-----------
        26

postgres=# SELECT extract(DOW FROM now());
 date_part
-----------
         1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;You can do much more with &lt;code&gt;extract()&lt;/code&gt;, for a complete list of examples &lt;a href="http://www.postgresql.org/docs/9.2/static/functions-datetime.html"&gt;take a look at the official documentation&lt;/a&gt;. Here&amp;rsquo;s just a few&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;day&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;century&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;dow&lt;/code&gt; (day of week)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;doy&lt;/code&gt; (day of year)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;minute&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;month&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;year&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;Converting between timezones&lt;/h3&gt;

&lt;p&gt;Sometimes it is useful to show a specific date in a different timezone, which is exactly what the &lt;code&gt;AT TIME ZONE&lt;/code&gt; construct is for. Let&amp;rsquo;s take a look at how it works. We&amp;rsquo;ll do this in a transaction so that the &lt;code&gt;now()&lt;/code&gt; function always returns a same value and we can easily see the difference in hours.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;postgres=# BEGIN;
BEGIN
postgres=# SELECT now();
              now
-------------------------------
 2013-08-26 12:39:39.122218+02

postgres=# SELECT now() AT TIME ZONE 'GMT';
          timezone
----------------------------
 2013-08-26 10:39:39.122218

postgres=# SELECT now() AT TIME ZONE 'GMT+1';
          timezone
----------------------------
 2013-08-26 09:39:39.122218

postgres=# SELECT now() AT TIME ZONE 'PST';
          timezone
----------------------------
 2013-08-26 02:39:39.122218
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</content>
  </entry>
</feed>
