Building Real-Time Web Applications

We're definitely going HTML5

This is a hands-on workshop that will teach you how to build a Web application that incorporate real-time communication between the client application running on the browser and the back-end server.

We will start with an overview of technologies and tools available for building real-time Web apps, what’s involved, the basics, and the gotchas. Next, we will build, in real-time, a real-time chat application using the python (Tornado) + socket.io + Backbone stack. Why not Node.js, you might ask. Simple: it’s too easy, too popular, and and not super-stable or secure. But you’re welcome to use Node.js as the backend in your own apps!

Conditional Expressions in Python

Historically, python didn’t have a special operator for a conditional expressions. The (condition ? true-value : false-value) expression is concise and convenient, and has been missed for years by many python programmers, especially those coming from programming backgrounds that support the conditional expression such as C/C++, Java, Perl, PHP, and friends.

Starting with python 2.5, python programmers worldwide rejoice as now we can compress the the following if-else statement:

into the following one-line expression:

But what’s with the ugly syntax? Seriously? Why would the condition go in the middle whereas in all other languages the condition always go in the front?

The answer to the ugliness question is given in PEP 308:

The decision was checked by applying the new syntax to the modules in the standard library and seeing how the resulting code read. In many cases where a conditional expression is used, one value seems to be the ‘common case’ and one value is an ‘exceptional case’, used only on rarer occasions when the condition isn’t met.

Now, how many python programmers in the world are working on the python’s standard library? And the rest of us have to endure this ugliness … NO!

Not all hope is lost. Even without explicit support for a conditional expression, python’s awesomeness comes to the rescue:

Now that is just plain elegance and beauty. And guess what? It works even in python prior to 2.5 so you don’t need worry about your code breaking on legacy systems (that is, if you’re unfortunate enough to work with python prior to 2.6).

As Daniel pointed out in the comments, there is a gotcha to this elegance: the true_value must not evaluate to False. More specifically, the expression will always return the false_value if the true_value is one of:

  • None
  • False
  • zero of any numeric type, for example, 0, 0L, 0.0, 0j.
  • any empty sequence, for example, ”, (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __nonzero__() or __len__() method, when that method returns the integer zero or bool value False.

Which syntax of the three options do you prefer? Why? Share your insights in the comments.

Backbone Marionette Filtered Collection Views

Backbone.Marionette is probably the best thing that ever happened to Backbone.

What is Backbone.js?

Backbone.js gives structure to web applications by providing models with key-value binding and custom events, collections with a rich API of enumerable functions, views with declarative event handling, and connects it all to your existing API over a RESTful JSON interface.

What is Backbone.Marionette?

Backbone.Marionette is a composite application library for Backbone.js that aims to simplify the construction of large scale JavaScript applications. It is a collection of common design and implementation patterns found in the applications that I (Derick Bailey) have been building with Backbone, and includes various pieces inspired by composite application architectures, such as Microsoft’s “Prism” framework.

The need for filtered collections

Let’s say I have a collection A that can be updated dynamically both client-side and server-side (items added, removed, resorted). Let’s ViewA be a CollectionView

ListView = new Marionette.CollectionView({collection: A});

Elements of A have an attribute is_starred. I’d like to provide a filter option in the UI that will cause ListView to show only starred elements of A. I know the general pattern of filtering A on the is_starred property to create a new collection AStarred and pass it to ListView. The problem is that when items are added or deleted to A, I also need them to be added/deleted to AStarred if they’re starred.

What’s a good approach to achieve this?

One approach is to create a custom collection StarredCollection that is initialized using A and listens on add/remove/reset events on A, and only adds/removes items that are starred, and then pass an instance of StarredCollection to ViewA.

Another approach is to create a custom view, StarredView, with wrappers around addItemView/removeItemView that only calls the corresponding methods in the CollectionView if the item passes a test. This could even be generalized to FilteredCollectionView where a filter is parameter, so one could filter on whatever.

I went with the second approach.

Here is how I did it.

First, patch Marionette’s CollectionView (I could’ve created a SortedCollectionView instead, but this was easier in my case).

Then, say I have CollectionView called ListView, and I want to create a filtered version of it:

Or, if I’m showing the ListView in a Layout’s regions, then I create StarredLayout:

But I also modified on the ListLayout.onRender to pass the filter to the list view before creating the ListView:

Warning: this breaks the handling of EmptyView which will be shown only when the original collection is empty, not when filtered collection is empty.

I hope others find this useful and suggest ways of improving it.

Theories of Limited and Unlimited Space

Is Space limited or Unlimited?There are two competing theories that try to define whether our space is bound.

Theory of Limited Space

The theory of limited space states that there is a limited amount of space available, and once the subject expands to fill all the available space, it will not be able expand any more.

Theory of Unlimited Space

The theory of unlimited space states that there is an unlimited amount of space available. As a result, and expanding subject can continue to expand indefinitely, not limited by the space constraint, but its expansion my still be limited by factors other than space availability.

The Third Theory of Space

There is a third little known theory that states that space is neither limited nor unlimited. One consequence of this theory is that an expanding object will not be permitted to expand indefinitely due to space constraints, but the amount of remaining available space is undefined.

Some scientists believe that all three theories are wrong, which leads to the fourth theory of space.

Fourth Theory of Space

The forth theory of space stats that space is both limited an unlimited. Although intuitively this theory sounds paradoxical, in reality it is sound and consistent.

P.S., sorry Albert, I didn’t have time – though I had the space – to incorporate time in the space theories.

This post was inspired by the brilliant comedy of Brian Malow.

eMail, e-mail, email, or Email?

This is getting really annoying, so I did some research. According to all the sources I could find, the proper form is e-mail (hyphenated), and at the beginning of a sentence, if you want to capitalize, it should be e-Mail (hyphenated with only M capitalized). It is a common name, not a proper name, so should not be capitalized in the middle of a sentence.

According to the style guides of “Get it Write”, IEEE, Webopedia, and the Associated Press, as well as the Merriam-Webster dictionary and the New Oxford American Dictionary, the proper spelling is e-mail. Some cite email as a possible alternative spelling.

One source stated that typical evolution of new terms removes all spaces and hyphens from the word, so email, online, and website are becoming the norm, although not official yet.

My conclusion is that although “e-mail” appears to be the correct form, “email” is also accepted, is shorter, and easier to capitalize at the beginning of a sentence. So I’m going to go with “email” as the officially correct spelling in my text moving forward.

As a side note, this means that I will treat website, webpage, and online as correct, single word nouns.

TitanFile – Secure Document Sharing and Tracking

TitanFile is a Web-based service that enable businesses to communicate and share files easily and securely. Our goal is to make secure document sharing as transparent and easy as using email, if not easier. Think of TitanFile as FedEx for electronic documents: a secure and reliable method to deliver a document from the sender to the recipient, with recipient verification, delivery notification, and reporting. Our target audience are businesses and government organization who care about the security of their file and the confidentiality of their clients.

I co-founded TitanFile with Milan Vrekic, who has won the 1st place prize in the recent Best Big Idea competition for his business plan for TitanFile. The competition was organized by the Nova Scotia Co-op Council.

The service is currently in closed beta. If you’re interested in gaining early access, you can make request.

I conclude this intro with a promotional video that Milan has put together.

Why am I here? Where do I need to go? And how do I get there?

42Thinking about the meaning of life, God, religion, right and wrong, the universe, and everything, many questions to come to mind. But at a personal, fundamental level, it all boils down to three questions:

  1. Why am I here?
  2. Where do I need to go?
  3. And how do I get there?

Most people focus on the first question: “why am I here?” They want their lives to have a purpose, or they believe they lives do have a purpose and they sincerely wish to find out what it is. Some have already come to terms with the first question, and at a very general level, they believe we are here to do something, or to achieve some objective – be it enlightenment, helping humanity, glorifying a supreme being, or just enjoying it while it lasts. For those, the focus is then on the second question: “where do I need to go?” In other words, while they have some believes or understanding about the general reason of why they are here, they want to know more specifically what it is that they need to achieve. Enlightenment – at what level? Helping humanity – to what extent? Glorifying a supreme being – to what degree? Enjoying it while it lasts – how much?

But I personally believe that if there was just one question the mattered, one question to which the answer would lead to all the other answers, the question would be the third one: “how do I get there?” In the process of getting there, I hope to understand why I am here. And once I get there, I hope to see where it is I was going. It’s the journey that makes up the difficult part, and is the secret to answering all the questions that we may have.

Wisdom from Social Media

Welcome to Our 21st Century!

Our Communication-Wireless
Our Phones-Cordless
Our Cooking-Fireless
Our Food-Fatless
Our Sweets-Sugarless
Our Labor-Effortless
Our Relations-Fruitless
Our Attitude-Careless
Our Feelings-Heartless
Our Politics-Shameless
Our Education-Worthless
Our Mistakes-Countless
Our Arguments-Baseless
Our Boss-Brainless
Our Jobs-Thankless
Our Needs-Endless
Our Situation-Hopeless
Our Salaries-Less&less

I don’t know who the original author is .. I saw this posted on Facebook — wisdom from Social Media.

How to count search results in Gmail

Gmail is a great products with powerful search and labelling systems that changed the way I use email. There is a small annoyance though – when doing search, Gmail does not return the total number of results if you have more than a few. Further, there is no easy way to access older results or see more than 20 results per page. Basically, you’d have to keep hitting the “older” button until you get to that last message. But what if you had hundreds or thousands of results from a mailing list, like I do?

Here is a power tip for Gmail users with thousands of messages on how to find the exact number of search results, browse 100 results at a time, and easily access the oldest messages:

  1. Do a search, either regular or advanced through the “Show search options” link.
  2. Select all the results by hitting “Select: All”
  3. Really select ALL the results by clicking on the “Select all conversations that match this search” link
  4. Apply a new label to the results, such as “Results Count”
  5. A popup dialog will ask you “This action will affect all conversations in this search. Are you sure you want to continue?” Hit OK.
  6. Now you can go to the “Results Count” label on the left side bar and:
    1. See the total number of messages
    2. See the total number of unread messages
    3. Paginate through the messages up to 100 messages at a time
    4. Access the oldest message easily through the now available “Oldest »” link.
  7. If you no longer need this label, you can select all the message and clear the label so it’s usable in future searches.

Did you find this tip useful? I’d appreciate some feedback in the comments. Feel free to link to other Gmail power tips.