Scott Hanselman

Your users don't care if you use Web Sockets

November 9, '11 Comments [28] Posted in HTML5 | Musings
Sponsored By

I had a lovely time at the Keeping It Realtime Conference this week in Portland. The conference was put on by the lovely folks over at &yet and I'm glad I met them. "KRTConf" was a whole conference dedicated to "real-time" web applications. This largely meant node but also other real-time frameworks including Damien and David's SignalR that Paul Batum (Paul owns Web Sockets for .NET) and I presented. I'll blog our demo soon.

Some folks in the audience wanted to make a real-time app today but really wanted to use Web Sockets today and had concerns about broad support. Coincidentally, I had just visited &yet's new site for product called "&!" or "AndBang," a collaboration tool for teams. However, when I visited the page I got this message: https://andbang.com/you-need-websockets-yo.

Your browser doesn't support Web Sockets, so be sad

Switching protocols from HTTP to WebSocketsIn the near future, all browsers and servers will support Web Sockets, a technique by which a persistent connection is negotiated over HTTP and then a protocol switch happens from http:// to ws://. However, not every browser or server is there yet (IE9, for example) and there's some arguing about versioning, etc. If you don't support Web Sockets, falling back to "Long Polling" is a way to get effectively the same behavior that works everywhere.

Long polling is a technique that lets it look like your browser has a "persistent connection" to the server when in fact you've just got a really "persistent client." By this I mean, rather than a connection that doesn't shut down (persistent) you have a client that will make a call and when it completes, it'll call back. The very definition of persistent, eh? See what I did there?

Persistent: Continuing firmly or obstinately in a course of action in spite of difficulty or opposition.

Long Polling is not as efficient but it works nicely and is a totally reasonable fallback when Web Sockets support is unavailable. The latest preview release of IE10 includes Web Sockets support. But my mom doesn't know about Web Sockets and shouldn't have to.

The 10,000 people on the planet that care about Web Sockets are not your customers, and while using Web Sockets might get you mentioned on TechCrunch, supporting only Web Sockets is a great way to cut your potential audience in half.

You can use the lovely CanIUse web site to find out if you browser supports something you'd like to use. Here's their Web Sockets table.

Table of Browsers and their support for Web Sockets

Your users don't care about Web Sockets. They care about delightful sites. We use JavaScript polyfills when our browsers don't support HTML5 features we want. We use jQuery when JavaScript support is dodgy between browsers. And our web frameworks should gracefully fallback to use Long Polling when Web Sockets isn't available.

Related Links

P.S. Yes, if your app is a real-time trading app or and needs down-to-the-millisecond timing, then sure, maybe you need Web Sockets. But your app isn't that app.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Useful Visual Studio Extension - Web Essentials from Mads Kristensen

November 8, '11 Comments [62] Posted in ASP.NET | Tools | VS2010
Sponsored By

Visual Studio 2010 is really extensible and that's allowed many folks on the team to try out new features for Web Development without having to rebuild Visual Studio itself. One of those "playground" extensions is called "Web Essentials" by Mads Kristensen. Mads handles HTML5 and CSS3 tools for our team. You might remember Mads from when we released the Web Standards Update a few months back.

Web Essentials is Mads' playground and it's pretty awesome. It's so awesome that I think you should check it out and then, *ahem*,  leave a comment on this post encouraging Mads and gentle urging his boss(es) to get these features into the next version of Visual Studio.

First, it adds a few nice touches to the CSS Editor in Visual Studio. As you can see below, it adds a little glyph next to each color that shows you the actual color.

A nice visual refresh to the Visual Studio CSS editor

If you hover over the colors, you'll get a preview with more details. You'll also get font previews on mouse hover.

Font Previews are fun

These are just nice little touches that make you smile. They also smooth little issues with my daily workflow. After having them, I miss them when they are gone.

There's also some more functional features, like embedding tiny images in your CSS files as base64. Sounds nuts, but if  your small icons are so small that the HTTP Headers are larger than the image itself this can be an easy way to remove an HTTP request.

Here I'm taking the tiny "Ajax Loader" PNG in my app and turning it into an inline image.

Embed Resource as base64

So those

Look at the tiny images as base64

It even has some evil little helpers help you use well-known CSS hacks to control visibility of rules. Like, if you hate yourself and use IE6. ;)

Enabling evil with IE6 hacks

You can minify just a selection of text if it makes you happy.

image

You can drag and drop images and automatically get a background-image CSS rule, which is a HUGE time saver. Oy. All this and a bunch more little subtle stuff.

The best feature and feature that should cause you to download it NOW is that you can do live CSS editing in Visual Studio with a real-time preview.

Fire up a CSS file and hit Ctrl-Alt-Enter. Then click Settings and hit "Live Update CSS." Bam. As you type it'll get updated in the browser. Love this.

image

Do you like these playgrounds? Do you want some or all of these features in Visual Studio? Sound off below.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Back to Basics: Daylight Savings Time bugs strike again with SetLastModified

November 6, '11 Comments [32] Posted in ASP.NET | Back to Basics | Learning .NET
Sponsored By

CC BY-NC 2.0 Creative Commons Clock Photo via Flickr ┬ęThomas Hawk No matter how well you know a topic, or a codebase, it's never to late (or early) to get nailed by a latest bug a over a half-decade old.

DasBlog, the ASP.NET 2 blog engine that powers this blog, is done. It's not dead, but it's done. It's very stable. We had some commits last year, and I committed a bug fix in February, but it's really well understood and very baked. My blog hasn't been down for traffic spike reasons in literally years as DasBlog scales nicely on a single machine.

It was 10:51pm PDT (that's Pacific Daylight Time) and I was writing a blog post about the clocks in my house, given that PST (that's Pacific Standard Time) was switching over soon. I wrote it up in Windows Live Writer, posted it to my blog, then hit Hanselman.com to check it out.

Bam. 404.

What? 404? Nonsense. Refresh.

404.

*heart in chest* Have I been hacked? What's going on? OK, to the logs!

l2    time    2011-11-06T05:36:31    code    1    message    Error:System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: utcDate
at System.Web.HttpCacnhePolicy.UtcSetLastModified(DateTime utcDate)
at System.Web.HttpCachePolicy.SetLastModified(DateTime date)
at newtelligence.DasBlog.Web.Core.SiteUtilities.GetStatusNotModified(DateTime latest) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SiteUtilities.cs:line 1253
at newtelligence.DasBlog.Web.Core.SharedBasePage.NotModified(EntryCollection entryCollection) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1182
at newtelligence.DasBlog.Web.Core.SharedBasePage.Page_Load(Object sender, EventArgs e) in C:\dev\DasBlog\source\newtelligence.DasBlog.Web.Core\SharedBasePage.cs:line 1213
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) while processing http://www.hanselman.com/blog/default.aspx.

What's going on? Out of range? What's out of range. Ok, my site is down for the first time in years. I must have messed it up with my clock post. I'll delete that. OK, delete. Whew.

Refresh.

404.

WHAT?!?

Logs, same error, now the file is a meg and growing, as this messages is happening of hundreds of times a minute. OK, to the code!

UtcSetLastModified is used for setting cache-specific HTTP headers and for controlling the ASP.NET page output cache. It lets me tell HTTP that something hasn't been modified since a certain time. I've got a utility that figures out which post was the last modified or most recently had comments modified, then I tell the home page, then the browser, so everyone can decide if there is fresh content or not.

public DateTime GetLatestModifedEntryDateTime(IBlogDataService dataService, EntryCollection entries)
{
//figure out if send a 304 Not Modified or not...
return latest //the lastTime anything interesting happened.
}

In the BasePage we ask ourselves, can we avoid work and give a 304?

//Can we get away with an "if-not-modified" header?
if (SiteUtilities.GetStatusNotModified(SiteUtilities.GetLatestModifedEntryDateTime(dataService, entryCollection)))
{
//snip
}

However, note that I'm have to call SetLastModified though. Seems that UtcSetLastModified is private. (Why?) When I call SetLastModified it does this:

public void SetLastModified(DateTime date)
{
DateTime utcDate = DateTimeUtil.ConvertToUniversalTime(date);
this.UtcSetLastModified(utcDate);
}

Um, OK. Lame. So that means I have to work in local time. I retrieve dates and convert them ToLocalTime().

At this point, you might say, Oh, I get it, he's called ToLocalTime() too many times and double converted his times. That's what I thought. However, after .NET 2 that is possible.

The value returned by the conversion is a DateTime whose Kind property always returns Local. Consequently, a valid result is returned even if ToLocalTime is applied repeatedly to the same DateTime.

But. We originally wrote DasBlog in .NET 1.1 first and MOVED it to .NET 2 some years later. I suspect that I'm actually counting on some incorrect behavior deep in own our (Clemens Vasters and mine) TimeZone and Data Access code that worked with that latent incorrect behavior (overconverting DateTimes to local time) and now that's not happening. And hasn't been happening for four years.

Hopefully you can see where this is going.

It seems a comment came in around 5:36am GMT or 10:36pm PDT which is 1:36am EST. That become the new Last Modified Date. At some point we an hour was added in conversion as PDT wasn't PST yet but EDT was EST.

Your brain exploded yet? Hate Daylight Saving Time? Ya, me too.

Anyway, that DateTime became 2:36am EST rather than 1:36am. Problem is, 2:36am EST is/was the future as 6:46 GMT hadn't happened yet.

A sloppy 5 year old bug that has been happening for an hour each year that was likely always there but counted on 10 year old framework code that was fixed 7 years ago. Got Unit Tests for DST? I don't.

My server is in the future, but actually not as far in the future as it usually is. My server in on the East Coast and it was 1:51am. However, the reasons my posts sometimes look like they are from the future, is I store everything in the neutral UTC/GMT zone, so it was 5:51am the next day on my file system.

Moral of the story?

I need to confirm that my server is on GMT time and that none of my storage code is affected my Daylight Saving Time.

Phrased differently, don't use DateTime.Now for ANY date calculations or to store anything. Use DateTime.UTCNow and be aware that some methods will freak out if you send them future dates, as they should. Avoid doing ANYTHING in local time until that last second when you show the DateTime to the user.

In my case, in the nine minutes it took to debug this, it resolved itself. The future became the present and the future last modified DateTime became valid. Is there a bug? There sure it, at least, there is for an hour, once a year. Now the real question is, do I fix it and possibly break something that works the other 8759 hours in a year. Hm, that IS still four 9's of uptime. (Ya, I know I need to fix it.)

"My code has no bugs, it runs exactly as it was written." - Some famous programmer

Until next year. ;)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Got clocks?

November 6, '11 Comments [34] Posted in Musings
Sponsored By

"Spring forward, fall back"
- The Idiot who invented Daylight Savings Time.

It's time to switch from Daylight Savings Time to Standard Time. I tweeted earlier that I'd gone around the house and updated, by rough count, 18 clocks. Then, I got a bunch of responses like "I've only got one," or "All mine are automatic," or "18 clocks, you're insane."

Turns out the real number is WAY more than 18. I didn't realize.

So, this is for all you folks with your microwave flashing 12:00. I decided to take a moment and really list out all the devices in my house that know the time, separating them by the ones I need to update myself and the ones that automatically update themselves. I realize I could NOT update 50% of them, if not more, and I realize I have a number of gadgets.

Fine, I like clocks. More perhaps than I realized. That said, I really only need the time right on the wristwatch I'm wearing at the time. For you young people, a wrist watch is a non-internet connected device worn often with a leather or metal strap that allowed you to glance as your arm and know the time. It did NOT have Siri, nor did it run Opera Mobile.

Most of these "don't matter" but it was the sheer number of time-keeping devices, both direct and indirect, that surprised me.

binaryclock Clocks I need to change manually

  • Six cheap wall clocks. In bedrooms, bathrooms, playroom.
  • Five clock radios. Bedrooms, guest room.
  • Stove
  • Microwave above stove
  • Insulin Pump
  • Two Blood Glucose Meters
  • Cool Digital Binary Clock on my desk (at right)
  • Two car radios with clocks
  • Zoom H4N Recorder (for my podcast)
  • Casio S95 Digital Camera
  • Canon SLR Digital Camera
  • Cordless Phone Base (3 satellite phones)
  • Nine wrist-watches (I collect watches)
  • Rainbird Water Timer

Clocks that are automatically updated

Of course, many of these are random Craigslist tech, but even if you remove those, you've still got a lot of things keeping time around the house. 

I bet you have more things in your life that care about the time than you think. How did we survive the Year 2000 again? Oh, none of these clocks matter, that's right.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Your images are a virus. They are EVERYWHERE on the Internet

November 4, '11 Comments [13] Posted in Blogging | Musings
Sponsored By

Stuff you love to do, Stuff you're good at, Stuff someone will pay you to do.This is a silly little story of a silly little image. An image whose original conceptual source isn't me. Well, I made the image, I made it in PowerPoint with Smart Art in about 12 seconds over 4 years ago. But per my G+ friend James Saull, I know now that the concept is called "The Hedgehog Principle" or "The Hedgehog Concept" by Jim Collins. You'll find it everywhere but it started in 2001 with the book "Good to Great." I likely absorbed it at some past point and when I got the job I drew three cricles. It's one of those "duh, that's awesome" concepts. It's just a Venn Diagram with three circles with the intersection, the middle bit, being the most awesome ideal part. This isn't about the original of the concept, it's how one image is found everywhere on the internet, spreading like a virus. Once you put an image on the internet, you'll never be able to take it down.

I made the circles for a blog post when I took the gig at Microsoft in July of 2007. I haven't thought about them much since, although I've used them in some presentations a few times. They are unimpressive and rather pink.

Yesterday at lunch I was on Facebook and commented on a friends photo. Her friend "liked" my comment, and I clicked to see who that friend of a friend was. Then just scrolling down, I saw my circles on this stranger's wall. Cool! What an amazing coincidence.

Dream Job on FB

This image was shared from another page within Facebook. I followed the rabbit. The photo had hundreds of likes and many shares.

Dream Job on FB

I then started wondering how far this thing went. Well, it spread long before this funny Facebook coincidence. Kyaw Zaw suggested on Google+ that I put my original image into Google Image Search.

Googling with a custom date range shows the first instance of my image, on my blog in 2007. You can also search for images with images using TinEye Image Search.

I can't see how to reliably hotlink to Google Image Search results, so go to http://images.google.com/, click the little camera icon and paste in the URL to the image, like http://www.hanselman.com/blog/content/binary/WindowsLiveWriter/2dc61d7e4a66_13443/image.png

Googling for Dream Job with Google Image Search

Widening the search dates to all dates, I can see there are 228 different places this image appears, mostly on career and inspirational/aspirational blogs.

Lots of Dream Job on the second page

Image search systems are a fascinating way to see how your images find their way around the web. If you had some intellectual property embedded within your images, presumably you could watermark your images but I suspect intuitively that heavily watermarked images might not spread as freely. There's just no easy way to "protect" (if you wanted to) your images these days. So they spread!

What images of yours have spread around the internet?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.