Tuesday, March 17, 2015

Step by Step Web Application Development

I get many questions on Quora where people ask how they can get into web application development. Everytime I used to find the correct sequence of my posts. So I though to summarize it in a post here itself.




If you are looking for mastering client side web application development, you need to start with basics at Introduction to Web Technologies. This talks about all aspects i.e. HTML, CSS as well as JavaScript.

If you are familiar with HTML and CSS and want to jump directly to basics of JavaScript, Javascript for Newbies is the place to start.

At this point I think it is good time for Getting Started with CoffeeScript. CoffeScript allows you to write neat code with simple syntax and verbs and compiles it into very good quality JavaScript code for browser. Or you may want to do it after jQuery as well.

Next I think you should learn Creating Web Applications using jQuery. jQuery is nothing but a library having a lot of functions for easing out your life. It is mostly syntactic sugar and standardization of interfaces across the functions as well as across the browsers.

If you like to go further advance you may want to take a look at Object Oriented Programing in JavaScript. There are some concepts you already know but they are presented differently in case of JavaScript.

Reaching this stage, you will start thinking of Single Page Applications (SPA) and will have a debate on which framework is good for developing SPA. The article Choice of Javascript MVC Framework: Backbone, Angular, Ember, Knockout etc. will not bring an answer for you but if you know nature of yourself and your application, it will help you find your answer.

From many SPA JavaScript frameworks, I choose 2 to talk about because I think they are of opposite nature.

Unlike other Angular JS presentations that pose it as a magic, Getting Started with Angular JS tries to find out how Angular is just a JavaScript library and still follows basics of HTML and JavaScript. Creators of Angular have creatively used some concepts to make it magical. This enables you also to create magic of your own.

I personally like BackboneJS more because it is not opinionated and gives more power to me rather than asking me to do stuff in a specific way. I know I need to write more code as compare to Angular to start with for normal use cases but I also know that I need to write less code as compare to Angular for specific use cases later. This is my personal bias and you should not be following that. Rather choose the framework that suites your situation better.

Creating Single Page Web App using Backbone JS step by step explains the process of creating a SPA using a sample application. The application code is available on Github and the application is also hosted for your use. This is a pure JS (and of course HTML and CSS) application that uses FourSquar and Google maps APIs as back end and creates a UI similar to Pintrest. The JS code is actually written in CoffeeScript but corresponding JavaScript code is also available. You can read whatever version you want and also compare them for ease of reading.


If you want to use JavaScript on server side as well, you may become interested in NodeJS.

Introduction to Node js talks about writing an application using the very basic and native NodeJS. You may never want to write a commercial application like this. The correct way is to use frameworks and middle wares. However, I recommend to read this to understand the philosophy behind NodeJS so that you can take a better decision which application you should write in Node and what precautions you should take.


Another useful stuff in JavaScript is CasperJS that is a wrapper over PhantomJS and is primarily used for testing UI programmatically. Testing Single Page Apps with Splinter and CasperJS talks about it briefly.

This concludes the Javascript stuff. Other posts you may find useful are:
There are other posts talking about solution to specific problems or explaining specific use cases but I am not sure if they are still relevant.


Saturday, June 7, 2014

Introduction to Node js

Another session I did as part of Pune JS meetup.

This covers the basics of NodeJs, how to set-up first NodeJs Application and always remember for Single-threaded Processes and keep writing non-blocking code.
It also covers difference in philosophy in comparison to NodeJs and other languages/web server design.
About 60 people joined the meetup.















Thanks a lot to people at Webonize lab for hosting the meetup and clicking photos :)

Monday, May 19, 2014

Choice of Javascript MVC Framework: Backbone, Angular, Ember, Knockout etc.

As usage (read fashion) of Javascript in increasing, I get a Question often in different forms:

Recently when I was thinking about it, was hungry and entered into kitchen, a very good analogy struck my mind:




"Which knife is best?"
So the question arises, "For what purpose?"

Each one of them is created keeping a specific purpose in mind and works best for that purpose. If you use one for some other purpose, it will work. It may work well but may not be best.

The same is applicable for technology frameworks. You need to choose one that is suitable for your app. While talking on this topic with people, I found that many of them choose a framework that is popular. Without considering what exactly the framework brings for them. The popularity may be just a hype at that time and I do not see it wise to make such an important product decision based on just hype.

However, if your app is just another app like others, you may want to go with the popular one. The JS Toolbox maintain a chart showing popularity parameters (Watchers, forks and open issues) for different JS frameworks using Github APIs. Here is the snapshot of May 16 2014.


I met a person who went for KnockoutJS because he was able to build first version very quickly using its data bindings. He still loves data bindings but when a user changes something on a form and later decides to Cancel instead of Save, he runs into issue as the model is already changed because of data-binding. Now he implements two models and on save copy from the UI bound model to the model that save the data to the back-end.

I answers the question in just one line, "We choose Backbone because we wanted power with us (programmers) than with the framework. We do not like magic in general."

Now, other than the type of app, personal style also adds to the making a choice.
On the web, I read so many reviews and comparisons people did. Most of them clearly write what they are looking in the framework and declare a winner from their point of view. 

For example, Gordon Hempton in his article "The Top 10 Javascript MVC Frameworks Reviewed" on CodeBrief says that he is looking for 1. UI bindings 2. Composed Views 3. Web Presentation Layer and 4. If it plays well with others like jQuery. After his review (and re-review), he says that emberJS is best as it is the only one providing all four together. Still, I would suggest reading the article as he lists one liner pros and cons of all the frameworks. You may discard his decision and take your own decision based on his inputs.

Similarly, Steven Sanderson has done a good and lengthy job of discussing points that one considers for such selection. In some cases he also discuss the points where people do not agree and have different opinions. Finally, he does not give any verdict but lists bulleted points about each framework. His post Rich JavaScript Applications – the Seven Frameworks (Throne of JS, 2012) is also worth considering while making a selection.

For making us choose right framework with a better hands-on feel, Addy Osmani (and friends) went ahead and created todomvc.com. It provides basic info about each framework, shows demo of ToDo List app using that framework and points you to a Github repository for code of the demo app. This gives a good feel of the framework even before you start and also allows you to compare as it is the same functionality implemented via different frameworks.

Addy also wrote a very good article on Smashing Magazine, Journey Through The JavaScript MVC Jungle. In the article, he not only discuss important points about selecting a framework (other than personal style and choice of features) but also suggests when to use what.

Here are a couple of quotes from his article:
BackboneJS
"I want something flexible which offers a minimalist solution to separating concerns in my application. ... I’d like some decisions about the architecture left up to me. ... As I may be building something complex, I’d like there to be an active extension community around the framework that have already tried addressing larger problems"
EmberJS
"I want something that tries to tackle desktop-level application development for the web. It should be opinionated, modular, support a variation of MVC, avoid the need to wire everything in my application together manually, support persistence, computed properties and have auto-updating (live) templates."
AngularJS
"I want something declarative that uses the View to derive behavior. It focuses on achieving this through custom HTML tags and components that specify your application intentions."
jQuery
"I want something that will help me build simple Web applications and websites. ... The solution should abstract away browser differences so I can focus on the fun stuff."

So the bottom line remains at its place :).
The selection of right framework is not that straight forward. It really depends on the nature of app and the developers.

The choice may go wrong for various reasons e.g. lack of big picture, time constraints etc. But keeping an eye and moving to a better suitable framework as soon as possible may help a lot.

In our case at ShopSocially, as we choose BackboneJS at the first place, we created our internal sub-framework as we realized a pattern and that is working beautifully.

Tuesday, April 22, 2014

Object Oriented Programing in JavaScript





JavaScript is a object oriented language. But does not work exactly same as other OO languages such as java or C++. 'Class' is a reserved keyword in JavaScript but it is not implemented.

Then how can we write object oriented code in JavaScript? What about inheritance as all?

JavaScript functions and objects can be used in many different ways and also JavaScript also supports something called prototypal inheritance. This allows you to write code that behaves like a class. The presentation here introduces this concept.

If you still want to write code the way you are more familiar i.e. defining a class using 'class' keyword and extending the class using 'extend', you can use CoffeeScript. I also have slides to help you move faster with CoffeeScript.




Sunday, April 13, 2014

Getting Started with Angular JS

Angular JS is a magical framework. It encapsulates so many concepts and technologies in itself.

Here is an effort for posing it less magical. In the attached presentation, I am trying to draw every Angular concept from what an average JavaScript developer already know.

The presentation is not complete i.e. it does not cover many aspects of Angular but hope this will help to start. Your comments and feedback will help a lot to improve.



Saturday, March 15, 2014

Creating Web Applications using jQuery

I have taken an online classroom session talking about jQuery and its usage. The program was organized by programmr.com.



The complete and correct reference is always jQuery API page, this session was just to introduce the topic so that attendees get a quick start in the web application development.




Thursday, March 13, 2014

Getting Started with CoffeeScript


CoffeeScript is a small language that compiles into very good JavaScript.

I have seen some confusion around against CoffeeScript saying:
-- Not every browser understands CoffeeScript by default
-- If we used CoffeeScript JS in browser, it is very hard to debug the code.

Both these sayings are true.

But I do not used CoffeeScript directly into the browser. Rather, I write code in CoffeeScript taking advantage of syntax etc. and compile it into JavaScript on my machine itself. So even my unit testing happens with good quality JavaScript code.

This was also part of the workshop series on creating single page web apps and helped people read the code example in BackboneJS presentation.




Sunday, March 2, 2014

Creating Single Page Web App using Backbone JS

During the third workshop on the topic, we created an application that allows user to select a place on world map, brings photos of interesting venues from FourSquare and shows them in tiled format.

The app is hosted as explore-venues and its complete code is available at Github.

Screenshot of Explore Venues


The presentation here helps build/read the code in a systematic way.



Saturday, February 15, 2014

Introduction to Web Technologies

I started a series of 3 workshops on Creating Single Page Web Application. First of the series introduces basics of web technologies.

Objective: Creating and manipulating simple web pages 
Prerequisites: Basic programing knowledge 
Program Outline: 
• Introduction to Web and its evolution 
• Web page basics and HTML tags 
• Styling elements 
• JavaScript basics and JSON 
• Introduction to DOM 
• Changing style using JavaScript 
• Simple DOM manipulation 
• Responding to user actions



Sunday, October 27, 2013

Using Google App Engine Python

PythonPune is a user meetup group for folks interested working in Python programing language. The group organizes knowledge transfer sessions on various topics.

This presentation was prepared for one such session held on Oct 26 2013. More than 20 Python programers (including a few students) attended the session.




During the session there was a discussion of free quota available to the apps. Following screenshots displays the current limits:

As you can see at the top, you can enable and disable billing for an application and quotas are reset every 24 hour. 
I did not find any programatic interface for getting current state of exhausted quota limits.

Google documentation for blobstore quota suggests that 5 GB blob storage is free. But it also says that blob storage also counts towards billable stored data. Free limit for billable stored data is just 1 GB.

Pricing documentation says after free limit, blobstore is available for $0.13 per GB per month and rate of stored data is $0.18 per GB per month.
Google Cloud Storage has no free quota and is chargeable $0.085 (or $0.063 for reduced availability) per GB per month.


 
 

Wednesday, October 2, 2013

Javascript for Newbies

A few people in PuneJS meetup group, asked to have a session covering basics of JavaScript. The registration for the event went up to 98+ and 30+ attended the session.
The session was held at CSP Pune hall and was attended by students, programmers as well as entrepreneurs.




The slides shown in the session are here for the reference.






Friday, September 20, 2013

Managing Files with GIT

CSI Pune chapter organized a full day workshop for educating people about using Version Control System (VCS) for organizing the files they work with. The session was not only for programers but was also for anyone who need to maintain files and their revisions e.g. Designers, Office Admins, Content Writers etc.

Here the content I prepared for the session, please do let me know your feedback/questions.


Wednesday, August 14, 2013

Differences between Facebook Open Graph and Facebook Newsfeed Stories

Again a question on Quora motivated me to writing a post here. Last time it was about difference between two like widgets and this time the question is:
Whats are the differences between Facebook Open Graph and Facebook Newsfeed Stories?
It is true that they look similar. Ultimately there is a photo with some title, caption and description.

However, the way the post are created as well as the way they stay on Facebook are different. Let's look at them one by one.

News Feed Stories

News Feed stories are the one you create either via Feed Dialog or using Graph APIs provided by Facebook. The concept of Feed, common usage and best practices are described at Feed Documentation page. While using graph API, one need to create a Post connection of User object.

All the items of the posts e.g. image, the link where it takes the user, title, caption and description is customizable. All the stories are independent and are not attached to any specific object or action.

Open Graph Stories

A Open Graph story is an action about an Open Graph object. The concept of open graph is described at open graph documentation page. One can turn any web page into an open graph object by putting open graph tags and start using pre-approved actions. For using custom actions, approval from Facebook is needed.

Because of the attachment with an object, the content of the post comes from the object (i.e. the web page) and can not be changed for every post.

However, a story generated by an Open Graph action is slightly better than any regular News Feed story in the sense that it not only shows up in news feed but also in ticker. Additionally, aggregation of Open Graph stories make it more powerful. Stories around similar objects or actions group together and rather than going deep in the timeline the post may surface up again and again as more actions happen.

EDIT Aug 16 2013 in reply to Yurly's comment:
If you are implementing for yourself, open graph stories should be preferred over news feed story because of its slightly better visibility. However, implementation may be tough and time taking [Please see my detailed answer on this topic at Quora]. Also there are many other factors that need to be taken care for increasing the interaction and conversion rate on a online retail website.
Third party companies focusing on these aspects (e.g. Shopocially) come as a great help for retailers.

Tuesday, July 30, 2013

Testing Single Page Apps with Splinter and CasperJS

Siddharth, Nikita and I delivered a session about testing modern Single Page Apps on Sat 27 July 2013. The meetup was organized by TechNext at Synerzip near City Pride Kothrud Pune.

Total of 96 people registered for the session. In spite of the rains, more than 30 came down.
Organizers told that this is the highest number they have seen for a testing related topic.

First part of the session was to introduce Single Page Apps. It also talked about how web has evolved and how Single Page Apps are different from traditional apps.

Next we discussed why old testing tools are not able to test Single Page Apps and what a new age tools should do to overcome the limitations.


During the solutions part Siddharth introduced Splinter, a Python based tool that launches the browsers and runs test scripts similar to old tools. However, the beauty is that the limitations of old tools are removed here.


Nikita then talked about CasterJS that allows tests to run in a headless browser. Other than being ultra fast, this gives the power of running multiple tests in parallel on a machine saving a lot of resources and time.

Overall the session was good. People were not only asking questions but also pitching in with the knowledge they had about tools and methodologies.
In spite of we took more time than expected, everyone was happy at the end. Response at the meetp page also says the same:




Testing singlepageapp from Akshay Mathur

Code sample for CasperJS is at http://pastebin.com/NuFKM85n
Code sample for Splinter us at https://gist.github.com/sidchilling/6112208

Wednesday, July 24, 2013

MongoDB workshop

I and Sarang took a full day workshop on MongoDB on Sat 21 Jul 2013. The workshop was organized by Pune Chapter of Computer Society of India.

Against our expectation, 30 people attended the workshop form different backgrounds.

While biggest group was of students, there were people from software industry, technical trainers and a professor from engineering college. From software industry, not only there were developers of all levels but also there were Software Architects, a General Manager and a Product Manager.


The day was divided into 3 parts.
First we talked about NoSQL databases, shift in their design paradigm, focused a little more on document based NoSQL databases and tried drawing some parallel from SQL databases.

Second part, after lunch, was a hands-on session of MongoDB using mongo shell. Sarang was instrumental telling more and more features of MongoDB and helping people to use them. The session was so exciting that people were not ready to go for the tea break.

At last we touched advance topics like data replication for disaster recovery and handling big data using map-reduce as well as Sharding.

Overall the workshops was received well and people are contacting us for the slides deck as well as delivering the same at their company and college.



Tuesday, April 23, 2013

Tile Puzzle

Image Courtesy:  Thought Collage
My 8 year old son found an old sliding tile puzzle and liked it a lot. This gave me the idea of having the soft copy of it. We searched and found so many of them on Internet. But nothing beats the joy of using your own creation and we (my son too) decided to implement one more for ourselves.

If you are not interested in the story and just want to play or look at the code, jump to the results.

Choice of Technology:
Guess what technology we choose for this?
We decided to keep it very simple. Another requirement was to use the same app on laptop as well as on phone.

Very quickly, the skeleton in HTML was ready. 
For implementing the functionality, I took two liberties. One was using jQuery instead of native Javascript and the other was using Coffeescript (and compiling into Javascript) instead of writing directly into Javascript.

Initial Cut:
My son got exited as I shown him the blank boxes :) but start getting bored when I was writing logic for getting the shuffled list of numbers. He started working with me as soon as I shown him the boxes filled with numbers. And he spotted a bug .... simple sequence number gotcha .... two squares were empty instead of one.

Enhancement Phase 1:
After I gave him initial cut to play, he came up with two enhancement requests:
  1. The hand pointer should show up only on movable tiles (actually it was a bug)
  2. Moving only one tile per click is cumbersome. Allow moving any number of tiles (max 3 is  possible) in one click.
Enhancement Phase 2:
Now as number puzzle started working fine he felt adding picture as it happens on the back of number puzzle. 

On the following morning, when I was talking about it to a programmer friend, he thought I did something very complex and technically challenging :) and fired so many questions:
You must have used a image processing library for creating small tiles out of a big picture. 
And when the puzzle completes, you must be rejoining the tiles to match it with the original picture. 
Does Javascript has support for Image processing as well?
What language you used at backend for this? What kind of APIs have you created? 


Image Courtesy: Satellite Signals

He felt some disappointment when I told that I did not do any fancy stuff and my puzzle went colorful with picture tiles just working with CSS. I used the image in background for each tile and used different background positions so that the correct portion of image is visible.

Enhancement Phase 3:
Kids (especially the movers one) get bored very quickly. This time he wanted to use his own picture instead of what I provided. So I just added a text box so that user can provide an image on the fly.
After completing one game he came back saying I should add the same code that runs in page load on the button click as well because the game is not restarting when he changes the image.

The Result:
As result of the exercise, I earned a lot of happiness and joy (and understanding of some logic) from my son, a puzzle hosted and is available to everyone to play, a code repository at Github, and this blog post :)

Monday, April 22, 2013

Cross-domain Communication .... for IE7 as well

While implementing Shopping Community feature, we need to have an iframe on merchant's site that shows the activity feed. As I told in the previous post, scroll bar in the iframe looks so ugly that we decided to have change the size of iframe based on the content inside the iframe.

This called for cross domain communication as the iframe was included on the retailer's website. On Googling we found 2 methods. One using iframes and other using html5 postmessage method.

As most of the browsers now (including IE9) support HTML5, we decided to go with it.

For IE7 and IE8, we implemented some hack exploiting Microsoft's old implementation of postmessage in IE that was different from the current standards.

Later we found a very good library called EasyXDM. The library supports many more browser versions. It adopts the implementation based on available features in the browser. For example, if it is a morden browser and postmessage is implemented in the standard way, it uses or it uses flash based implementation. More can be found in EasyXDM Readme at Github.

Friday, June 22, 2012

Positioning popup in a long iframe

Image: http://www.happynews.com


You must have got from the previous posts that we were involved in the development of Shopping Community that shows up on the retailers' website or within Facebook in an iframe. Shopping Community is actually an activity feed of users' action on products. By default, a feed tile has limited information and when user wants to see details, we open a light-box popup.

The Challenge
The popup showing details must be positioned in a way so that it appears in center of the window (read viewport). When the page was opened in the browser window, the solution was very simple.

popup_top = window.scrollTop + (screen.height - popup_height)/2

But as soon as it came into the tall iframe, window.scrollTop always remains 0 because the window in iframe never has a scrollbar. It is the parent window that has a scrollbar but the Javascript code inside iframe does not have access to that; again because of cross-domain communication issue.
When user has scrolled down in the page, popup opens in somewhere near the top :(. We can scroll the page back to the popup position by putting an anchor but that creates a bad user expirience for 2 reasons:
1. An unexpected jerk that is introduced by moving the page up.
2. User has lost the place where she was after closing the popup.

The Solution
So it was very important to figure out some way of positioning the popup such the popup shows up in the middle (well as best as possible)  in front of user's eyes without changing the scroll position of the page.

The only way out was to figure out the current scroll position. One was of course using cross-domain communication but doing without that was much better and reliable. BTW, as this was to be done within the iframe, we could use jquery, backbone and coffscript.




As you can see the code above, we captured the pageY and screenY properties of the click event object. This actually tells co-ordinates of the point where user clicked. As name of the properties suggest, the co-ordinates are with respect to page and with respect to screen. By subtracting them, we estimated the amount of scroll. This is not accurate but works for practical situations.

Now, it was simple to use the same formula to find out the popup top.

Hope this helps.
If I am not clear in my post, please shoot questions.

Tuesday, April 10, 2012

Don't kill setAutoResize, it works...

Image courtesy Softicons

In fact it does not work :(
If you have ever developed an iframe based canvas app for Facebook, you know this.
You may say,
"Well .... it works but not as the name suggests. It just grows the iframe height but never shrinks. That is why Facebook has decided to change its name to setAutoGrow :)"
However, there is a way to make setAutoResize work as the name suggests. [How can I tell this to Facebook developers?]

Read on if you are interested in full story.

The Problem:
When you create a web application that runs in an iframe on the web property of someone else, you are limited by the space provider for the layout, placement and sizing.
Facebook is one such space provider. It provides the space in form of iframe so that the apps can embed functionality within Facebook.

When the content in the iframe grows, ugly scrollbars show up.
For the new design of Shopping Community feature of ShopSocially, there was a need to resize the frame.

The Struggle:
Solution is to monitor the height of the content inside the iframe and communicate it to the parent window. The script in the parent window listens to the message and sets the height of the iframe.

I started with communicating $(document).height() [yes I had the luxury of using jquery here] but some browsers [you know who .... :)] were still showing scrollbar. So I thought of adding 30 extra pixels to the height.
Now, every time $(document).height() returns those 30 px added and I was adding 30 more :(. This was a real auto-grow.

Using $('body').height() solved the issue of auto grow but it stopped growing even when there was an absolutely positioned light-box popup :(.

The Hope:
Facebook has provided a method [FB.canvas.setAutoResize()] to the applications for starting a timer that monitors the size of the content in the iframe and communicate it to the parent window. Facebook script in the parent window listens to the message and sets the size of iframe.

However, the FB.canvas.setAutoResize() method works in a way that the height of iframe only grows and never shrinks. So rather than fixing the issue, Facebook has renamed the method to FB.canvas.setAutoGrow() that better describes the behavior :(.

The discovery:
Anyways, I thought of looking at the implementation done by Facebook guys. The code in _computeContentSize function looks somewhat complicated but works same as the jquery code I was using. Even the comment by the developer of that code says the same.

However, I observed that right edge of every element was being determined and max of it is being communicated as width. Ohh .... so this was the source of my Dancing UI problem. I don't know why but same was not happening for height.

The Solution:
I decided to give it a try. For my code I determined the max of bottom of the popup element and the page and used that as height.

Bingo!! That actually worked for me. Size of the iframe grows as content adds to it. If you go to the bottom and view all comments of a product, you will see a popup and the iframe grows to make the complete popup visible. The beauty starts when you close the popup, the iframe shrinks back to fit in the content size.

If it works for me, it will work for Facebook too and app developer's will smile :)

Saturday, April 2, 2011

Using Facebook Login on 3rd Party HTTPS pages

Update: The problem is solved by Facebook. So there is no need to do what is written here.

In the last post, I was discussing how having Facebbok connect login on https page is painful.

A few days back, I also observed a notice form Facebook saying that the bug has been fixed.
However, looking at the history of Facebook in this regard, we are still using our method of logging mechanism. Our system also provides us a few other advantages over Facebbok connect login.

Now that we have solved all the issues, it looks very simple ... :)

The workflow:
  1. On a third party HTTPS page, user is presented some info to share with her friends via our app
  2. When user clicks on share button, she will be asked for Facebook login and permissions
  3. The info gets passed to our server after successful login and server does a Facebook wall post
  4. User is presented a personalized page in the iframe 

Problems:
As discussed in last post. IE should not show any HTTPS warning.

Solution:
Here are the steps in short:
  1. Remove inclusion of Facebook connect JS library (all.js)
  2. When user clicks on share button, open a new window for Facebook login process (this step is not completely client side, server is also involved)
    1. URL of new window is the auth url of Facebook
    2. Return uri is some page on your server that handles login process with Facebook on server side, implements your own session state for the user and returns your session information to the client
  3. When login process is complete, the window passes session information to the opener (the page in iframe) and closes itself
  4. The page in iframe completes rest of the workflow
Stay tuned for the details and code examples....
Cheers....