Tuesday, June 19, 2007

Digg wins news industry sites comparison

During the last 3 years there has been an enourmous shift in the news industry from classic news sites such as CNN.com and BBC.co.uk to web2.0 news site made of User generated content, such as Digg, Delicious and StumbleUpon. I checked these 5 sites on alexa to see how they do head to head.

first I looked at the 1 year graph. Everyone seem very stable, with CNN and BBC leading, Digg takes the middle, followed by Delicious and StumbleUpon. The weekend effect is apparent at 3 of the sites.

when I looked at the 3 Year graph, i could easily see that CNN and BBC where at approximatly the same ranking, where as the web2.0 sites were nowhere in site 3 years ago. After 2 years of rising sharply Digg and Delicious (strangly matching all the way) leveled around 100, 150. Another thing is that CNN and BBC started declining 2 years ago, and they keep going slowly but steadily. It so happened that Digg and CNN has very similar rank, as one takes the others place.

I expect great things for digg, and I think they will lead the revolution of web2.0 many years to come.

CNN is dead, Long live Digg ;-)


Monday, June 18, 2007

Designing Business Objects for web2.0 Applications

When we sit down at the beginning of the process to design our brand new shiny web2.0 application (Please, not another social network...) we should consider several issues regarding Object Oriented design in general, as well as web2.0 application specifically.

Find your objects
This is true for every program. First thing to do is read the product specification (if working in medium or large company) or just describe the system in words. Notice the objects in your sentences as candidates for being objects in the system. For each one ask yourself: "Does it contain any information i should use?" This Information is the object properties. "What actions can this object perform?" leads to a list of methods. Objects on the list that have no properties or methods should not be objects in the system.

Remember the Channels
When selecting parameters for the methods and types for the properties remember who is on the other side. Is that a web page calling? A web service? Some Embedded code from this blog?
What are they asking for? What language do they speak?
You may consider writing a Data Access Layer that speaks SQL with the database and sends DataSet objects to the Business Objects. The business objects receive requests from Server-Side code that runs inside a web server. Those requests use a certain textual format to pass data to the server. Simple applications may send single values over Query Strings, but any real application should use JSON or XML as a data protocol with the web client. On this web client you may like to have some cool AJAX controls to smooth the User Experience, So you will need a set of Client-Side objects, written usually in Javascript.

Design Serializable Objects
If you are using JSON or XML you should design your objects to be serializeable. That means using only string, int, bool, object and array as the types of your properties. Any JSON Serializer can read such objects and this saves you iterating through all the object properties to read it's values. You just write something like MyJSONString = Serializer.Serialize(MyObject) and Bam! you got yourself a JSON string to send back to the browser.

Match Server and Client Objects
Since we have two sets of objects, one on the server and one on the client, we should match their properties to allow for smooth data transfer. There are JSON and XML serializers in javascript too, of course. You can avoid this problem all together by using a Library planned for this purpose like the Microsoft Ajax Extensions 1.0. Take a look but be careful, this package is only for professionals. It requires the best understanding of Internet technologies and much patience for Microsoft heavy object models.

Plan Great Utilities
Well, What do we do with all this? We can start by writing a set of utilities that will simplify our coding and will make the application more robust. One function we should write is a generic DataSet2Object function, that reads a dataset and returns a collection of the desired objects. There may be some others, but you get the idea.

Remember to breath :)


Wednesday, June 13, 2007

Reporting Page Url from Flash video player = Monitization

Recently the web got at least one more layer in the spaghetti of sites we are used to. Since the rising of the social networks and the User Generated Content sites everyone is providing the users with tools to embed parts of their web site in the user's blog, profile or other personalized web space. Now instead of many sites operating in solitude we now have one web page that includes information from many web sources.

A more recent development was the rich media, Images and video mainly. Most video sites now allow their users to embed their videos everywhere (except of MySpace... but that is another story that will probably have it's own post.). How can the video site owner know when someone is playing his video on some blog or profile? Remember that most of the video sites business model are commercial-based, and the commercials should run along-side with the video content, So targeting your embedded audience even just for GeoLocation can be worth a lot.

Most video players are written in flash, and the communication between Flash and web server never was that amazing. There are several things you need to do in order to have a Flash application report to a web server the Url of the page it is running inside.

First you need to use an ActionScript command called ExecuteExternalCall to call a javascript that reports the Page Url with the property document.location.href.
Set the AllowScripts attribute of the embed tag to "Always", and make sure that the hosting site allows those scripts to run. MySpace doesn't, so do try to execute any Javascript in any form on MySpace.

this will take you to a point where everything works fine in FireFox but no information received from explorer browsers. To allow this info to run through IE you need to add the Flash ClassId attribute to the embed tag, and remove the Type attribute (should be there before, with Flash/Application or something like that in it's value). At this point we got the Url from IE as well but our application stoped working.

We are still resolving this issue, and we might try sending Ajax request to the server straight from the "ExecuteExternalApplication" function, because we realized that the function call works fine, only getting the value back in ActionScript fails.

I'll update on this when we have more info, also I will appreciate any comments on how to handle such a trick.


Tuesday, June 5, 2007

The place of Intuition in the Development process

I'm developing software for the last 10 years, and operating computers for 20. I have watched endless programmers, team leaders, project managers and CTOs bang their heads (or do other stupid things) with anger and frustration every time they observed the non-disputable fact of life that computers don't always do what we want them to. Hell, they sometimes don't even do what they were asked to do (Which is something completely different from what we expect them to do.)

This is a universal truth and software developers can benefit the most from integrating it into our systems: Things don't always work as we expect. The difference between us is what we do with this fact.

As i see it there are mainly two ways to handle this. Some ignore reality and stick to their opinions. We can see these guys shouting and hitting their computers and some of them develop habits of "saying YES and doing NO", always being late and other behaviors that express repressed anger. On a more professional level you may notice some programmers that insist on sticking to their bugs and can spend days with them, even if they could solve them quickly. By struggling with their work they achieve much self feedback like "I'm working so hard" and "This job is so difficult, I must be so smart to be doing it".

Others accept reality and recognize the emotions that this reality stir in them. Those people will usually act according to decisions and not emotions. They also can be stressed or relaxed in their work, but they will at least do a genuine effort to make things better both professionally and inter-personally. The amount of stress that we acquire in work is directly connected to the way we recognize and process our feeling and emotions during the day of work. If we know how to harness these powerful emotions to our use we can achieve so much more then if we are fighting the emotions, working hard to keep them inside.

The development of software is a very complex task, constructed of many many little tasks that we perform and put together in order to create a complete product or feature. All this complexity it done using a very, very, very and some more complex hardware that includes billions of tiny electrical components. I wonder it actually works at all ! No wonder it has it's glitches. To me it reminds a sentence from chaos theory that states that systems become more chaotic as they become more complex. I believe that computers are so complex that they conform to this rule.

Those that accept reality looks at every bug as an opportunity to improve, to learn new things, a challenge that needs to be met, not a problem that needs to be solved. Such state of mind creates a relaxed, interesting, fruitful working environment. This is the right ground to promote creative thinking and creative solutions. With such inner position we can learn new things easily and enjoy the learning. After all, if there are no more bugs our work is done. The essence of our work is bug fixing. This is what we do. Everyone can create bugs. not all of them can solve them.

After a while working in this state of mind one starts to write less, but more efficient code. One is found more time thinking then hitting on keys, but get more work done faster. When it comes to approaching new challenges, one starts to use it's intuition. Sometimes we "just know" things. It is stupid to ask a beginner to use Intuition. He should work by the rules for a while. But after a while, the solutions start popping up like popcorn. There are many ways to do each task and choosing the right one is many times an intuitive decision. We usually don't have time to explore all the options well, and we have to choose. These decisions should be taken by an experienced person, and he will be stupid NOT to use intuition in his decisions.

Our intuition is our highest inner voice (emotions-thoughts-intuition) and only by using it can we achieve self-completion in our life and in our work.

I know, there is no code sample with this post. I think developers can still benefit from it...



Thursday, May 31, 2007

ASP.NET AJAX Control Toolkit

Check out the new Ajax controls from Microsoft, featured on CodePlex, their open source site. They have some neat solutions to tasks that we had some hard time doing before this toolkit came to be, and they are giving it away for free right now. On the site check out the live demo they have, it show samples to all the controls.

I have not started to use it yet, but my company 5min.com intends to use it from now on, so I will be reporting about progress and problems with this kit, and probably some code samples of my own.

If any of you out there has any experience with this toolkit or with asp.net Ajax library I would like to hear how it is going for you.


Friday, May 18, 2007

Using JSON to connect Flash with Asp.Net

Life of a programmer are becoming more and more simple when it comes to finding professional help online. Almost any question i have regarding my work, like how to do this, why that doesn't work, etc' I can always find the answer one google search away. Few clicks later i have an explenation, code sample or other solutions. This week i tried to do something that had no good samples or documentation. That made me feel like i have reached the limits of existing technology, and know i'm trying things that other never did before. It is quite exciting for me.

This week Alexey and I wrote a simple "Invite a friend" feature. There is nothing exciting about that in itself. The feature was written inside a Flash control and the business object that actually sends emails is part of our site's engine written in Asp.Net.
Http communication between Flash and web pages is common, and can be done by using the LoadVars object in ActionScript code. You can see a good example here. There are practicaly 3 ways to transfer data over Http to Flash objects. One is sending the data "as is", which is good when you pass one parameter or two. Two is using XML to pack the data on one side and unpack it on the other side. Three is using JSON as the data structure standard. I chose JSON because the code required to Serialize and Deserialize JSON objects and strings is much simpler, shorter, more intuitive and more object oriented. You will see in the samples that one line of code is required in every side of the interaction to persist objects over http between Asp.Net and Flash ActionScript, or any other two sides for that matter.

The c# code for creating Json object on the server is available here and uses Newtonsoft Json.Net objects.
The Actionscript code however, is not available on the internet yet, only here. this code recieves the Json object inside Flash and reads it's values like this: ( I got this code from Trannie who wrote the Json ActionScript 2 object.)

In my projects I used LoadVars to grab the JSON string, so my JSON file
had a NAME=VALUE format with VALUE being the JSON string:

client= {
"name" : "Client",
"crop" : { "x1" : 3, "y1" : 3, "x2" : 623, "y2" : 472 },
"link" : {
"type" : "site",
"url" : " http://www.clientsite.com"
"title" : "Job Name",
"text" : "This is a string describing the project",
"details" : [
"full" : "Browse.jpg",
"thumb" : "THB_Browse.jpg"
"full" : "HSWebShot2.jpg",
"thumb" : "THB_HSWebShot2.jpg"

Hopefully that is still valid JSON. It's an excerpt from a JSON file,
so I may have introduced some typos..

In AS, I'd do something like:

var client:Object;
var json = new JSON();

var lv:LoadVars = new LoadVars();
lv.onLoad = function(success) {
if(success) {
client = json.parse(this.client);

trace(client.name );

We had some trouble on the ActionScript side because we confused the LoadVars Http object with the internal Json object, so let me elaborate on this. the LoadVars object is the one that use Load method to send Http request to the server, and the onLoad callback function that recieves the response from the server. In order to send Json string in such a request you should use ActionScript syntax to wrap your Json string. The response stream should include the "client =" part, which is NOT generated by the Json server-side class. This part is actually not part of Json, but is used by the Flash LoadVars object in order to read this Json string in ActionScript.

Finally, i want to hear from you what kind of uses can you see for such technology that connects Flash applications with Asp.Net objects so smoothly and easily.

Until next time,


Wednesday, May 9, 2007

Using JSON in AJAX based applications

Who the hell is Json ?
Ajax is a very popular way to create dynamic web2.0 style web pages. It is a method of making asynchronous Javascript calls from web pages to web servers, allowing web pages that refresh different parts of the page with server data without loading the whole page every time. Ajax is used for many different tasks from news reels to rating widgets and much more. The Ajax interface is simple and open. Send an Http request with parameters info, and receive a call-back with the Http response. You can send anything both ways, but when you need to move large chunks of complex data you are on your own. You have to define the data structures, take care of any errors that happen, and so on.

Json stands for JavaScript Object Notation. Json is a protocol (like Xml is) for defining data passed through Ajax requests and responses. Json does not have to be used only with Ajax, it can be used to define javascript objects out of any data received to the browser. But Json is at it's best with Ajax, which demands some standard protocol for moving data through it.

Json is supported on many platforms, like other open standards, and I will share my experience using it with Asp.Net and Visual Studio.

Who likes Json ?
Json is good for any Ajax situation that uses more then one or two parameters. The sites that make the most out of it are those built Object Oriented. Json is a simple way to maintain in Javascript Client code the same Object structure that you use on the server for getting the data and using your business-objects. If you keep to the same objects structure even through your Xhtml code and Css files, you will get a very flexible and stable site.

Implementing Json
Because Json is a client-server protocol there are two sides to the implementation :)
For server side i use Newtonsoft Json.NET. It is a Dll file and Xml file that reside in your bin directory of the site, and allows you (after creating the proper reference) to write this kind of code in your server pages:

Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string output = JavaScriptConvert.SerializeObject(product);
//  "Name": "Apple",
//  "Expiry": new Date(1230422400000),
//  "Price": 3.99,
//  "Sizes": [
//    "Small",
//    "Medium",
//    "Large"
//  ]
Product deserializedProduct =
(Product)JavaScriptConvert.DeserializeObject(output, typeof(Product));

This code is a sample of how to convert any custom object that you use in your site to a string in Json format. This string is passed back to the client inside the Ajax response. The last line of code shows how to reverse the process, and convert Json strings that are received from the client into server objects.

On the client side things look much the same, to the surprise of many Javascript programmers, me included.
In order to parse the Json strings into Javascript objects on the client I use the json.js file from Json.org. It includes a Json parser. The client side code for reading a Json string will be:

var myObject = myJSONtext.parseJSON(filter);
Then you can use myObject to access all his properties as they were on the server. Json supports any structure that includes objects with pairs of name-value properties inside them, and arrays of these objects, in any depth of nesting.
When you are done juggling these objects on the client you can send them back to the server, maybe with updated data or even send totally different objects. anyway, the code will look like this:

var myJSONText = myObject.toJSONString();

This is pretty much the basics of json, but there is much more, so expect more on Json soon.

If you are using Json in your work please reply and tell me how is it working for you, and what you are using it for.

Benny is inlove with Json :)

Friday, May 4, 2007

Creating and Submitting a Sitemap to Google

As many know, Google search results are based on a crawler software that surfs our sites like a regular user, more or less. If for some reason we have pages on our site that are not linked from any other page, they will not appear in search results. In order to improve google's crawling in our site we should send google a link to an Xml file that we create in our site, and in this file we can list all the site's pages that we want Google to show in his search results. This file is called a Sitemap.

If a site is made of static pages, you just write them down in your file and you are done. All you need to do now (After submitting the Sitemap, of course) is to update the file with any new pages that you create. If, on the other hand, you are running a dynamic site that pulls it's pages out of a database, you have more work to do.

First, you need to create a Permalink for every page. Permalink is a static Url, without ? and parameters in the Url. You can use any unique value for that, like product name or page title.
A permalink can look like this: http://www.5min.com/Video/What-is-5min%20XH9DUGl7fO8%3d.
Then you need to generate the sitemap from a server page. You can use the XmlDocument object model to build the correct Xml file format. I suggest that you use an HttpHandler for this task, but any Asp page will do as well. All you need to do is to write the Xml file into the Http Response object.

Now that you have the Sitemap ready, you have to submit it to google as your Sitemap. For this you have to register to the Google Webmaster Tool service. If you already have a Google account then it's enough. There you have simple instructions how to submit your Sitemap. You can submit many sites and see how google reads them and more statistics.

You can find good information on Sitemaps at http://www.sitemaps.org/.
An already-made c# Sitemap class can be found here.

Please comment with your opinion on this post.


Sunday, April 22, 2007

Debugging Javascript in Visual Studio

Last week our server crashed, and had us roaming the internet with no purpose for a couple of days. Maybe one day I will write about that a separate post. The outcome of this is that all Internet Explorer definitions were reset when i connected to the new server and domain.

Since then, i was unable to debug javascript in visual studio. It took me a while to verify that I'm trying to debug the correct script and that the calling procedure is working fine.

I made sure that IIS was configured to debug asp pages, as you can see in the following pictures.
Inside the IIS console open the web site properties.

On the "Virtual directory" tab click on "Configuration".

In the configuration dialog select the Debugging tab and check the two debugging flags.

The last thing to check was Internet Explorer Options. In order to debug Javascript inside Visual Studio one must uncheck the (default) options that disable javascript debugging both on Internet Explorer and on other browsers. this is the checkbox to uncheck:

Well, Now you can just break an any Javascript line, see all values inside the Visual Studio enviroment, and generally have fun while debugging your site :)
benny is freely debugging javascript...

IFrame strikes back

Well, the IFrame bug from last post still exist, and we still don't know what to do about it.
Some good news are that this bug now appears only seldom and we are less concerned about it. We created a random query string that we attached to this frame source url, to make sure that the browser really really really not caching the page. this way the browser thinks its a new page every time. This (at least we think so) lowered to frequency of appearance of the bug to reasonable levels. Yet, the mystery continues...

Thursday, April 12, 2007

IFrame object loads wrong pages !?!?!

This is truly a bug from hell.

For the last two days the upload page on our site behaves very strange. If it was not so creepy i would say the page is sick :-)
this page includes an IFrame object that shows a second page inside, we will call them Outer page and Inner page.
What happends is this: when entering the outer page we sometimes see the inner page correctly. Other times we see the inner page broken, and the source shows that not all page was loaded. Other times we see even stranger things: the inner page is partially loaded and a totally different page is appended to it's end, creating a total mess on the page.

this bug appears only on our production server (dedicated, hosted) and not on our internal network. We tried to move the javascripts on the page around with no change. We tried putting a meta tag that prevents caching of the page - again nothing changed.

We are going to try a sniffer on the response and compare it with the browser source, maybe something goes wrong while rendering this page.

I have not fount any documentation of such a bug anywhere on the net. I still didn't get any answers from any newsgroup or from Experts Exchange (a great technical solution site). I'm getting a little worried...

If anyone ever encountered such a problem or has any suggestions i will be happy to hear it.
I will keep rolling this story as it happends.

Scratching my head :-)

Flash object fails to call Javascript function

The most popular way to show video in web pages today is to play the video files inside flash players that are embedded inside the html page. Like everyone else we did it too and it works just fine.

A new feature that we added to our site is the ability to embed our flash player into any blog or site page, and for this purpose we also added a feature that sends information from the flash player to our server each time someone plays one of our videos somewhere out there. That way we can count how many views our videos get even outside the site.

When we added this view-counter feature we could not get it to work. The flash player did not reach the server page even though we wrote the calling code properly.

After looking at some professional blogs and sites we found out the problem: we use the name "5minPlayer" as our player tag name, and the Object tag that holds the flash player should not begin with a number...

There are other limitations to this object tag, and also many things you should know if you are going to use flash on your page, especially if you are implementing extensive interaction between flash and the html page. A great page on this issue can be found here.

Wednesday, March 21, 2007

Browser Compatability

As we all know (and don't like it...) different browsers display HTML pages differently. We always work hard to make the pages we write to look good and the same in all browsers, or at least in the more popular ones. Some browsers that we will not mention their name (but they are made by microsoft) show HTML differently even between version. some not-very-sophisticated code works in one version and not in the other, especially new code is not backward competible.

There are two ways to tackle this situation. One is to write "lower denominator code", HTML code that all browsers and versions treat the same, and avoiding code that the browsers show differently. This leaves us with a pretty basic subset of HTML, but it can be done for simpler pages.

The second way is to write browser-specific code. something in the area of "If IE then do this, else if Firefox do that". this should be done only in cases where there is no unified solution that include all browsers.

In order to apply the second solution on the server side i suggest using these components and methods:

Create an Enum for all different supported browsers and version, something like that:

public enum Browsers

Create a function that recieves the BrowserCapabilities object included inside every HTTP request, and returns the specific browser and version as one of the Enum members:

public Enums.Browsers GetBrowser(HttpBrowserCapabilities browserCap)
string strBrowser = browserCap.Browser;
int majorVer = browserCap.MajorVersion;
return (Enums.Browsers)Enum.Parse(typeof(Enums.Browsers), strBrowser + majorVer.ToString());

Use this function to easily create switch cases to handle places that need different treatment for each browser: (In this case i use a negative margin to eliminate a blank area created in the original of a relatively-positioned Div element that includes an overlay image. This bug happends only in IE version 6)
switch (utils.GetBrowser(Request.Browser))
case Enums.Browsers.IE6:
divPlayImage.Style.Add("margin-bottom", (0- imgPlay.Height - 3).ToString() + "px");

Happy coding,


Friday, March 16, 2007

Formatting durations

As the internet gets filled with media files more and more sites need to display the duration of the audio/video file next to the file name and description. We store a file's play duration as a number of seconds in the database. In order to display the duration in a x:xx format we need to format it, using this small function:

private string FormatDuration(short seconds)
string result = string.Empty;
int minutes = seconds / 60;
int secondsLeft = seconds % 60;
result = minutes.ToString() + ":" + secondsLeft.ToString("00");
return result;

Thursday, March 15, 2007

Parsing tags input using Split function

User input validation is one of the more complex issues in developing a solid data-entry form. Input validation is the basis of application security, database integrity, application stability and more.

Today i encountered an input validation issue regarding the input that users enter as "tags", the popular new type of information used in many User Generated Content (UGC) sites.

When i used the function Split from the String class to parse the tags into an array of strings i found out that if the user entered multiple space characters (eg. "tag1 tag2 tag3") each space from the second one will result in an empty cell in the tags array. Splitting these sample tags will result in this array:
array[0]: "tag1"
array[1]: "tag2"
array[2]: ""
array[3]: ""
array[4]: ""
array[5]: "tag3"

In order to avoid entering blank tags in the database i wrote a simple function that prepares a clean array of tags from the raw tags string. Here it is:
(Besides removing empty cells this function also convert commas to blanks and lowers the case for all tags. This avoids multiple-word tags and case differences, as our project demands)

private string[] PrepareTags(string tags)
char[] delim = { ' ' };
//Replace comma with blank
tags = tags.Replace(",", " ");
//Split to array with blank delimiter
string[] tagsArray = tags.Split(delim);
ArrayList tagsList = new ArrayList();
//Eliminate empty cells
string[] preparedTags;
for (int i = 0; i < tagsArray.Length; i++)
if (tagsArray[i].Trim().Length > 0)
//Make all tags lowercase
preparedTags = new string[tagsList.Count];
return preparedTags;

If you find this function useful please comment and tell me.

Monday, March 12, 2007

Cached Ajax Response

On my project i have a nice Upload wizard. It allows a user to write file information while the file is uploading. This page uses Ajax calls in order to communicate with the server while the file is still uploading.

I had strange behaviour with this page...
Sometimes it worked and sometimes not...
While debugging i found out that if i upload the same file twice the second time fails.
After placing breakpoints i found that the Ajax requests that fail never reach the server side.
This fact gave me the idea that the response is being CACHED by the browser.

In order to prevent Ajax requests from being cached by the browser i put these lines in my code before writing the response:

Response.CacheControl = "no-cache";
Response.AddHeader("Pragma", "no-cache");
Response.Expires = -1;
Response.ContentType = "text/plain";

This solved the problem entirly.

P.S. If you are asking for static data using Ajax you may want it to be cached on the client to shorten response time. In that case don't use this code.