Scott Hanselman

Penny Pinching in the Cloud: How to run a two day Virtual Conference for $10

April 26, '13 Comments [28] Posted in Azure | Open Source | SignalR
Sponsored By
DotNetConf Logo

We've just finished Day One of "DotNetConf" our community-run free online conference for developers who love the .NET development platform and open source!

UPDATE: All the Videos from both days of DotNetConf are now online and available for on-demand viewing!

The Conference Platform

It seems funny to call the software our conference runs on a "platform" as that sounds very "enterprisey" and official. In the past we've done aspConf and mvcConf with sponsors who helped pay for things. We used Channel 9 and had a studio and streamed either from Seattle or using Live Meeting.

However, this year we wanted to do it on the cheap and more distributed. We wanted speakers from ALL over in all time zones. How cheap? About USD$10ish we figure. I'll get a complete bill later, but we basically wanted to scale up, do the talks and scale down.

Video Broadcasting and Screen-sharing

  • This year we are using Google Hangouts with their "Hangouts On Air" feature. A "dotnetconf" Google Account invites the presenter to a Hang Out and check the "on air" box before start the hangout. Then we use the Hangout Toolbox to dynamically add on screen graphics and speaker labels. Everyone sets their resolution to 1280x768 and the live stream ends scaling down to 480p.
  • Once you hit "Start Broadcast" you're given a YouTube link to the live stream. When you hit End Broadcast, the resulting video is ready to go on your YouTube page within minutes. The hangout owner (me or Javier) then clicks "Hide in Broadcast" and fades away. You can see I'm faded away in the below screenshot. I'm there, but only if I need to be. When there's only one active presenter the Hangout turns into a full screen affair, which is what we want.
  • Important Note: Rather than an 8 hour Hangout, we started and stopped as each speaker did their talk. This means that our talks are already discrete on the YouTube page. The YouTube videos can have their start and end times trimmed so the start isn't so rough.

Google Hangouts On Air

The Database

Surprise! There is no database. There is no need for one. We're running a two page site using ASP.NET Web Pages written in WebMatrix. It runs in the Azure cloud but since our dataset (speakers, schedule, the video stream location, etc) isn't changing a lot, we put all the data in XML files. It's data, sure, but it's a poor man's database. Why pay for more than we need?

How do we update the "database" during the talk? Get ready to have an opinion. The data is in Dropbox. (Yes, it could have been SkyDrive, or another URL, but we used DropBox)

Our Web App pulls the data from Dropbox URLs and caches it. Works pretty nice.

<appSettings>
<add key="url.playerUrl" value="https://dl.dropboxusercontent.com/s/fancypantsguid/VideoStreams.xml" />
<add key="url.scheduleUrl" value="https://dl.dropboxusercontent.com/s/fancypantsguid/Schedule.xml" />
<add key="url.speakerUrl" value="https://dl.dropboxusercontent.com/s/fancypantsguid/Speakers.xml" />
<add key="Microsoft.ServiceBus.ConnectionString" value="Endpoint=sb://[your namespace].servicebus.windows.net;SharedSecretIssuer=owner;SharedSecretValue=[your secret]" />
</appSettings>

The code is simple, as code should be. Wanna show the schedule? And yes , it's a TABLE. It's a table of the schedule. Nyah.

@foreach(var session in schedule) {
var confTime = session.Time;
var pstZone = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var attendeeTime = TimeZoneInfo.ConvertTimeToUtc(confTime, pstZone);
<tr>
<td>
<p>@confTime.ToShortTimeString() (PDT)</p>
<p>@attendeeTime.ToShortTimeString() (GMT)</p>
</td>
<td>
<div class="speaker-info">
<h4>@session.Title</h4>
<br>
<span class="company-name"><a class="speaker-website" href="/speakers.cshtml?speaker=@session.Twitter">@session.Name</a></span>
<br>
<p>@session.Abstract</p>
</div>
</td>
</tr>
}

Scaling Out

Scaling DotNetConfWe've been on an extra small Azure Website and then switched to two large (and finally, two medium as large was totally overkill) web sites. 

We scale up (and hence, pay) only during the conference and turn it down to Small when we're done. No need to spend money if we don't need to.

Scaling DotNetConf to Large

Updating the Site in Real-time with SignalR

Because the YouTube link changes with each Hangout, we had the problem that attendees of the conference would have to hit refresh themselves to get the new URL. There's a number of solutions to this that I'm sure you're already thinking about. We could meta refresh, refresh on a timer, but these aren't on demand. We also wanted to show a few videos during the downtime. One of us preps the next speaker while the other queues up videos to watch.

We realized this was a problem at about 10pm PST last night. Javier and I got on Skype and came up with this late night hack.

What if everyone had SignalR running while their were watching the videos? Then we could push out the next YouTube video from an admin console.

So visualize this. There's the watcher (you), there's a admin (me) and there's the server (the Hub).

The watcher has this on their main page after including the /signalr/hub JavaScript:

$(function () {
var youtube = $.connection.youTubeHub;
$.connection.hub.logging = true;

youtube.client.updateYouTube = function (message, password) {
$("#youtube").attr("src", "http://www.youtube.com/embed/" + message + "?autoplay=1");
};
$.connection.hub.start();

$.connection.hub.disconnected(function () {
setTimeout(function () {
$.connection.hub.start();
}, 5000);
});
});

The watcher is listening, er, watching, for a SignalR message from the server with the YouTube video short code. When we get it, we swap out the iFrame. Simple and it works.

Here's the admin console where we put in the next YouTube code (I'm using Razor in ASP.NET Web Pages in WebMatrix, so this is mixed HTML/JS):

<div id="container">
<input type="text" id="videoId" name="videoId"><br/>
<input type="text" id="password" name="passsword" placeholder="password"><br/>
<button id="playerUpdate" name="playerUpdate">Update Player</button>
</div>

@section SignalR {
<script>
$(function () {
var youtube = $.connection.youTubeHub;
$.connection.hub.logging = true;

$.connection.hub.start().done(function () {
$('#playerUpdate').click(function () {
youtube.server.update($('#videoId').val(), $('#password').val());
});
});
$.connection.hub.disconnected(function() {
setTimeout(function() {
$.connection.hub.start();
}, 5000);
});
});
</script>
}

We put in the short code, the password and update. All this must be complex eh? What's the powerful SignalR backend up running in the cloud backed by the power of Azure and Service Bus look like? Surely that code must be too complex to show on a simple blog, eh? Strap in, friends.

public class YouTubeHub : Microsoft.AspNet.SignalR.Hub
{
public void update(string message, string password)
{
if (password.ToLowerInvariant() == "itisasecret")
{
Clients.All.updateYouTube(message);
ConfContext.SetPlayerUrl(message);
}
}
}

This is either a purist's nightmare or a pragmatists dream. Either way, we've been running it all day and it works. Between talks we pushed in pre-recorded talks and messages, then finally when the live talk started we pushed that one as well.

We also updated the DropBox links with the current Video Stream so that new visitors showing up would get the latest video, as new site visitors wouldn't have been connected when the video "push" message went out.

image

What about scale out? We sometimes have two machines in the farm so we need the SignalR "push updated youtube video message" to travel across a scale-out backplane. That took another 10 minutes.

Scaling out with SignalR using the Azure Service Bus

We used the SignalR 1.1 Beta plus Azure Service Bus Topics for scale out and added an Azure Service Bus to our account. Our app startup changed, adding this call to UseServiceBus():

string poo = "Endpoint=sb://dotnetconf-live-bus.servicebus.windows.net/;SharedSecretIssuer=owner;SharedSecretValue=g57totalsecrets=";   
GlobalHost.DependencyResolver.UseServiceBus(poo,"dotnetconf");
RouteTable.Routes.MapHubs();

Now SignalR uses the Service Bus Topics for "Pub/Sub" to pass notifications between the two web servers. I can push a new video from Web 1 and it is sent to everyone on Web 1 and Web 2 (or Web N) via SignalR's realtime persistent connection.

image

We'll delete this Service Bus Topic as soon as we are done. I would hate for the bill to get up into the nickels. ;) Here's example pricing from the Azure site:

432,000 Service Bus messages cost 432,000/10,000 * $0.01 = 44 * $0.01 = $0.44 per day.

I'm not sure how many messages we've done, but I can rest assured it won't cost a pile, so that's a relief.

Thank you to the Community!

  • Big thanks to designer Jin Yang who created the dotnetConf logo and design. Tweet @jzy and tell him you think he is special. Thanks to Dave Ward for converting Jzy's design into HTML!
  • Kudos and thanks to Javier Lozano for his coding, his organizing, his brainstorming and his tireless hard work. It was also cool for him to sit with me for hours last night while we hacked on SignalR and the DotNetConf.net site.
  • Thanks to David Fowler for saying "it'll just take 10 minutes to add Service Bus." 
  • Thanks to Eric Hexter and Jon Galloway for their organizational abilities and generous gifts of time on all the *conf events!
  • But mostly, thanks to the speakers who volunteered their time and presented and the community who showed up to watch, interact and play with us!

Sponsor: The Windows Azure Developer Challenge is on.  Complete 5 programming challenges for a chance at spot prizes, Stage prizes and the Grand Prize. Over $16,000 is up for grabs with 65 chances to win!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am 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 ORCS Web

Building Web Apps with ASP.NET Jump Start - 8 Hours of FREE Training Videos

February 28, '13 Comments [41] Posted in ASP.NET | ASP.NET MVC | ASP.NET Web API | Screencasts | SignalR | Speaking
Sponsored By
image

Last week Jon Galloway, Damian Edwards and myself (with a raspy throat) were up in Redmond at the Microsoft Campus filming at Microsoft Virtual Academy.

They've got a whole studio there so we spent the whole day presenting LIVE. There were several thousand folks watching live and interacting with

Very special thanks to Brady Gaster and ASP.NET community members Scott Koon, Peter Mourfield, and Rob Chartier who were furiously handling questions in the chats! Your volunteerism and dedication to the community is deeply appreciated! Let's give them a hand, eh?

Jon worked very hard to put together a great day of content based on the successful Web Camps classes we've given all over the world. We took all this and worked to update it with all the new improvements in the ASP.NET and Web Tools 2012.2 release last week so it's very up to date.

Building Web Apps with ASP.NET Jump Start: (01) What's New in ASP.NET 4.5

Building Web Apps with ASP.NET Jump Start: (01) What's New in ASP.NET 4.5

This module will review what's new in ASP.NET 4.5. It will provide an overview of strongly typed data controls and model binding in web forms, friendly URLs, page inspector, Visual Studio Web Editor features and much more.

 

Building Web Apps with ASP.NET Jump Start: (02) Building and Deploying Websites with ASP.NET MVC 4Building Web Apps with ASP.NET Jump Start: (02) Building and Deploying Websites with ASP.NET MVC 4

In this session the instructors go over ASP.NET MVC 4 and provide several demos on creating a new site; adding a model, controller and view, to using entity framework code first. Lastly they demo how to deploy to Windows Azure Web Sites.

 

Building Web Apps with ASP.NET Jump Start: (03) Creating HTML5 Applications with jQueryBuilding Web Apps with ASP.NET Jump Start: (03) Creating HTML5 Applications with jQuery

This module introduces you to the new standards of HTML5 and provides a demo of how powerful it is. Additionally you will see how it works with ASP.NET MVC 4, jQuery overview, Visual Studio Web Tools, Web Essentials and SPLA Template.

 

Building Web Apps with ASP.NET Jump Start: (04) Building a Service Layer with ASP.NET Web APIBuilding Web Apps with ASP.NET Jump Start: (04) Building a Service Layer with ASP.NET Web API

Have you always want to know how to build a service layer with ASP.NET Web API? This segment shows how ASP.NET Web API fits in, and how to consume Web API from jQuery and Windows 8.

 

Building Web Apps with ASP.NET Jump Start: (05) Leveraging Your ASP.NET Development Skills to Build Office Apps Building Web Apps with ASP.NET Jump Start: (05) Leveraging Your ASP.NET Development Skills to Build Office Apps

Get ready to see several Demos leveraging ASP.NET skills to build apps for Office specifically using HTML 5+ jQuery and ASP.NET Web API. This module will also go into further details regarding apps for Office and how they work. Using jQuery inside Office is freaky and cool.

 

Building Web Apps with ASP.NET Jump Start: (06) Building and Leveraging Social Services in ASP.NET Building Web Apps with ASP.NET Jump Start: (06) Building and Leveraging Social Services in ASP.NET

In this session you will see how to using social authentication with ASP.NET as well as an overview of the new Facebook application template.

 

Building Web Apps with ASP.NET Jump Start: (07) Building for the Mobile Web Building Web Apps with ASP.NET Jump Start: (07) Building for the Mobile Web

This module will provide and overview of adaptive rendering in ASP.NET 4.5 and ASP.NET MVC 4. This is especially important since mobile is fast becoming the primary way people browse the web. We'll also cover jQuery Mobile.

 

Building Web Apps with ASP.NET Jump Start: (08) Real-time Communication with SignalR Building Web Apps with ASP.NET Jump Start: (08) Real-time Communication with SignalR

In this segment the instructors go over SignalR, and an incredibly simple real-time web for .NET. It will also provide an overview for real-time hit counter, what SignalR is and how to build a chat application, a multi-player game and load balancing SignalR.

 

Building Web Apps with ASP.NET Jump Start: (09) Taking Advantage of Windows Azure Services Building Web Apps with ASP.NET Jump Start: (09) Taking Advantage of Windows Azure Services

And where would we be if we could not scale it all up or down. This flexibility can be provided with Windows Azure. Here you will see how Windows Azure fits in with mobile services, virtual machines while managing caching and storage.

 

I hope you enjoy the day! Here's a complete course outline with jumps to specific spots:

Building Web Apps with ASP.NET Jump Start

If you’d like more information, including links to a lot of the sample code, see Jon’s wrap-up post.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am 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 ORCS Web

Visual Studio 2012 and .NET Framework 4.5 is RELEASED - Here's 5 minute videos to get you up to speed quick

August 15, '12 Comments [92] Posted in ASP.NET | ASP.NET Ajax | ASP.NET Dynamic Data | ASP.NET MVC | Open Source | SignalR | VS2012
Sponsored By

Jason Zander announced today that Visual Studio 2012 and .NET Framework 4.5 is RELEASED to web. The .NET Web Tools Team (the team I'm on) has coverage on their blog as well. I thought I'd showcase some Tiny Happy Features that the team worked on just because it made life better. Some are large some are small, but all are tiny happy features.

I'll continue for a few more Tiny Happy Features over the next few weeks but this last week I took some time and recorded 13 (ya, thirteen, oy) short videos to show you guys these features in action. These are SHORT videos that are at most 4 to 6 minutes. It's hard to watch 60 to 90 minute screencast so I did these little one-take quick shots so you could watch them at lunch.

If you watch all these videos it will take you less than an hour and you'll have a good practical idea of what's new in Web Development and Tools with Visual Studio 2012. This is by no means exhaustive, but it's a lot.

The other concept that's worth pointing out is One ASP.NET. We've pulled the Web Tooling and Templates out into extensions in Visual Studio 2012. This means we can update Web Tools without updating all of Visual Studio. I talked about this in the One ASP.NET keynote at aspConf. We'll be updating the tools - not in major scary ways - but in useful and important ways that make front end web development easier. We'll look at small updates either quarterly or maybe semi-annually so when a new technique comes out you don't have to wait for the next version of Visual Studio.

Download Visual Studio 2012

MSDN Subscribers can download now at the MSDN Subscriber Download Page. For volume licensing customers, Visual Studio 2012 products will be on the Volume Licensing Service Center tomorrow. If you want to download Visual Studio 2012 free trial versions, or to download the free Express versions, head over to the the Visual Studio product website.

Azure SDK for both Visual Studio 2012 and Visual Studio 2010

It's also worth noting that the Windows Azure .NET SDK has been updated today as well and you can download versions for either Visual Studio 2010 SP1 or Visual Studio 2012.

The Videos

Here's the 13 short videos showing each of my favorite features in just a few minutes. There are new Entity Framework 5 videos as well, so be sure to scroll all the way down!

CODEC NOTE: These are using HTML5 video and MP4. If you are using a browser that doesn't support that codec, click the Header links to go to the ASP.NET site directly and the videos will stream with Silverlight.

Model Binding

HTML Editor

CSS Editor

JavaScript Editor

Page Inspector

ASP.NET 4.5 Web Forms Strongly Typed Data Controls

Web Publishing Improvements

ASP.NET MVC 4

ASP.NET Web API

Bundling and Optimization

SignalR and Web Sockets

Async and Await

OAuth in the Default ASP.NET 4.5 Templates

Entity Framework 5

There are also 5 new Entity Framework videos done by Rowan Miller that show new features of the new Entity Framework 5 as well as walk you through Code First vs. Model First vs. Database First. They are excellent screencasts and I recommend them.

I want to write code:

I want to use a visual designer:

EF5 is the newest version of Entity Framework. These short videos and step-by-step walkthroughs will get you started with the new EF5 features

  • Enum Support in Code First - The domain classes that make up your Code First model can now contain enum properties that will be mapped to the database.
  • Enum Support in EF Designer - Using the EF Designer you can now add enum properties to your entities.
  • Spatial Data Types in Code First - Spatial data types can now be exposed in your Code First model using the new DbGeography and DbGeometry types.
  • Spatial Data Types in EF Designer - Spatial data types can now be used in the EF Designer using the new DbGeography and DbGeometry types.
  • Table-Valued Functions - Table-valued functions (TVFs) in your database can now be used with Database First models created using the EF Designer.
  • Multiple Diagrams per Model - The EF Designer now allows you to have several diagrams that visualize subsections of your overall model. This allows larger models to be broken up into multiple smaller diagrams. You can also add color to the entities to help identify sections of your model.

Related Links

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am 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 ORCS Web

ASP.NET for Mobile, One ASP.NET and Realtime ASP.NET with Signalr - Video of Scott Hanselman's talks in Russia

June 5, '12 Comments [24] Posted in ASP.NET | ASP.NET MVC | Javascript | SignalR | Speaking
Sponsored By

Hey that says Scott Hanselman in Russian!I was outside Moscow last week speaking at Microsoft DevCon 12 in Russia. They did a great job with the event and not only filmed everything but did picture in picture as well as real-time translation into Russian. It was also live streamed at the team and later edited for download. Very cool. Big thanks to the team for putting the videos up so fast!

The English versions of my three talks (plus one open Q&A sesssion) are now all up for your viewing pleasure. If it seems I'm speaking ever so slightly slower than usual, that was at the request of the translators, and is a good practice when speaking English to non-native speakers.

You can also download and view the Russian versions if you like as well.

ASP.NET for Mobile Phones and Tablets

The ASP.NET for Mobile Phones and Tablets video

Download links:

SignalR and the Realtime Web

The SignalR and the Realtime Web video

Many problems, many solutions: One ASP.NET

The Many problems, many solutions: One ASP.NET keynote video

Open question and answer session – Ask Scott Hanselman

The Open question and answer session – Ask Scott Hanselman video

Hope you enjoy them!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am 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 ORCS Web

Solving the Shakespeare Million Monkeys Problem in Real-time with Parallelism and SignalR

November 12, '11 Comments [26] Posted in ASP.NET | Javascript | SignalR
Sponsored By

A monkey with a skull. Oh yes.A little over 18 months ago I was talking to Stephen Toub (he of the Parallel Computing fame) about parallelism and the kinds of problems it could solve.

I said, naively, "could we solve the million monkey's problem?"

He said, "the what?"

"You know, if you have an infinite number of monkeys and an infinite number of keyboards they will eventually write Shakespeare."

We brainstormed some ideas (since Stephen is a smarter than I, this consisted mostly of him gazing thoughtfully into the air while I sat on my hands) and eventually settled on an genetic algorithm. We would breed thousands of generations of (hypothetical) monkeys a second and then choose which ones would be allowed to perpetuate the species based solely on their ability to write Shakespeare.

We used the .NET 4 Task Parallel Library to make it easier for the algorithm to scale to available hardware. I mean, anyone can foreach over a million monkeys. But loops like that in parallel over 12 processors takes talent, right? Well, kinda. A lot of it is done for you by the Parallelism Features in .NET and that's the point. It's Parallel Processing for the Masses.

We created a WinForms version of this application and I've used it on and off to demonstrate parallel computing on .NET. Then Paul Batum and I went to the Keeping It Realtime conference to demonstrate SignalR this last week. I didn't want to do the same "here's a real-time chat app" or "here's a map that shows its results in real-time" demos that one always does at these kinds of things. I suggested that we port our WinForms Shakespeare Monkey demo to ASP.NET and SignalR and that's what Paul proceeded to do.

Looks like 80,000 generations of monkeys

When doing something that is crazy computationally intensive but also needs to return real-time results you might think to use node for the real-time notification part and perhaps spawn off another process and use C or something for the maths and then have them talk to each others. We like node and you can totally run node on IIS or even write node in WebMatrix. However, node is good at some things and .NET is good at some things.

For example, .NET is really good at CPU-bound computationally intensive stuff, like, I dunno, parallel matrix multiplication in F# or the like. ASP.NET is good at scaling web sites like Bing, or StackOverflow. You may not think IIS and ASP.NET when you think about real-time, but SignalR uses asynchronous handlers and smart techniques to get awesome scale when using long-polling and scales even more in our labs when using an efficient protocol like WebSockets with the new support for WebSockets in .NET 4.5.

So, we wanted to see if you combined asynchronous background work, use as many processors as you have, get real-time status updates via SignalR over long-polling or Web Sockets, using C#, .NET 4.5, ASP.NET and IIS.

It takes about 80,000 generations of monkeys at thousands of monkey generations a second (there's 200 monkeys per generation) to get the opening line of Hamlet. So that's ~16,000,000 monkeys just to get this much text. As they say, that's a lot of monkeys.

Here's the general idea of the app. The client is pretty lightweight and quite easy. There's two boxes, two buttons and a checkbox along side some text. There's some usual event wireup with started, cancelled, complete, and updateProgress, but see how those are on a monkey variable? That's from $.connection.monkeys. It could be $.connection.foo, of course, as long as it's hanging off $.connection.

Those functions are client side but we raise them from the server over the persistent connection then update some text.

<script src="Scripts/jquery-1.6.4.min.js"></script>    
<script src="Scripts/json2.min.js"></script>
<script src="Scripts/jquery.signalR.min.js"></script>
<script src="signalr/hubs"></script>
<script>
$(function () {
$('#targettext').val('To be or not to be, that is the question;\nWhether \'tis nobler in the mind to suffer\n\The slings and arrows of outrageous fortune,\n\Or to take arms against a sea of troubles,\n\And by opposing, end them.');

var monkeys = $.connection.monkeys,
currenttext = $('#currenttext'),
generationSpan = $('#generation'),
gpsSpan = $('#gps');

monkeys.updateProgress = function (text, generation, gps) {
currenttext.val(text);
generationSpan.text(generation);
gpsSpan.text(gps);
};

monkeys.started = function (target) {
$('#status').text('Working...');
$('#targettext').val(target);
$('#cancelbutton').removeAttr('disabled');
};

monkeys.cancelled = function () {
$('#status').text('Cancelled');
$('#cancelbutton').attr('disabled', 'disabled');
};

monkeys.complete = function () {
$('#status').text('Done');
$('#cancelbutton').attr('disabled', 'disabled');
};

$.connection.hub.start({}, function () {
$('#startbutton').click(function (event) {
$('#status').text('Queued...');
monkeys.startTyping($('#targettext').val(), $('#isparallel').is(':checked'));
});

$('#cancelbutton').click(function (event) {
monkeys.stopTyping();
});
});

});
</script>

The magic start with $.connection.hub.start. The hub client-side code is actually inside ~/signalr/hubs. See how that's include a the top? That client-side proxy is generated based on what hub or hubs are on the server side.

The server side is structured like this:

[HubName("monkeys")]
public class MonkeyHub : Hub
{
public void StartTyping(string targetText, bool parallel)
{
}

public void StopTyping()
{
}

}

The StartTyping and StopTyping .NET methods are callable from the client-side via the monkeys JavaScript object. So you can call server-side C# from the client-side JavaScript and from the C# server you can call methods in JavaScript on the client. It'll make the most sense if you debug it and watch the traffic on the wire. The point is that C# and Json objects can flow back and forth which blurs the line nicely between client and server. It's all convention over configuration. That's how we talk between client and server. Now, what about those monkeys?

You can check out the code in full, but StartTyping is the kick off point. Note how it's reporting back to the Hub (calling back to the client) constantly. Paul is using Hub.GetClients to talk to all connected clients as broadcast. This current implementation allows just one monkey job at a time. Other clients that connect will see the job in progress.

public void StartTyping(string targetText, bool parallel)
{
var settings = new GeneticAlgorithmSettings { PopulationSize = 200 };
var token = cancellation.Token;


currentTask = currentTask.ContinueWith((previous) =>
{
// Create the new genetic algorithm
var ga = new TextMatchGeneticAlgorithm(parallel, targetText, settings);
TextMatchGenome? bestGenome = null;
DateTime startedAt = DateTime.Now;

Hub.GetClients<MonkeyHub>().started(targetText);

// Iterate until a solution is found or until cancellation is requested
for (int generation = 1; ; generation++)
{
if (token.IsCancellationRequested)
{
Hub.GetClients<MonkeyHub>().cancelled();
break;
}

// Move to the next generation
ga.MoveNext();

// If we've found the best solution thus far, update the UI
if (bestGenome == null ||
ga.CurrentBest.Fitness < bestGenome.Value.Fitness)
{
bestGenome = ga.CurrentBest;

int generationsPerSecond = generation / Math.Max(1, (int)((DateTime.Now - startedAt).TotalSeconds));
Hub.GetClients<MonkeyHub>().updateProgress(bestGenome.Value.Text, generation, generationsPerSecond);

if (bestGenome.Value.Fitness == 0)
{
Hub.GetClients<MonkeyHub>().complete();
break;
}
}
}
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}

If he wanted, he could use this.Caller to communicate with the specific client that called StartTyping. Inside ga.MoveNext we make the decision to go parallel or not based on that checkbox. This is where we pick two random high quality parent monkeys from our population for a potential future monkey. Hopefully one whose typing looks more like Shakespeare and less like a Regular Expression.

By simply changing from Enumerable.Range to ParallelEnumerable.Range we can start taking easily parallelizable things and using all the processors on our machine. Note the code is the same otherwise.

private TextMatchGenome[] CreateNextGeneration()
{
var maxFitness = _currentPopulation.Max(g => g.Fitness) + 1;
var sumOfMaxMinusFitness = _currentPopulation.Sum(g => (long)(maxFitness - g.Fitness));

if (_runParallel)
{
return (from i in ParallelEnumerable.Range(0, _settings.PopulationSize / 2)
from child in CreateChildren(
FindRandomHighQualityParent(sumOfMaxMinusFitness, maxFitness),
FindRandomHighQualityParent(sumOfMaxMinusFitness, maxFitness))
select child).
ToArray();
}
else
{
return (from i in Enumerable.Range(0, _settings.PopulationSize / 2)
from child in CreateChildren(
FindRandomHighQualityParent(sumOfMaxMinusFitness, maxFitness),
FindRandomHighQualityParent(sumOfMaxMinusFitness, maxFitness))
select child).
ToArray();
}
}

My 12 proc desktop does about 3800 generations a second in parallel.

Hexacore Super Computer!

Big thanks to Paul for the lovely port of this to SignalR and to Stephen Toub for the algorithm. 

The code for the SignalR monkeys demo is on my BitBucket. Right now it needs .NET 4.5 and the Visual Studio Developer Preview, but you could remove a few lines and get it working on .NET 4, no problem.

Note that that SignalR works on .NET 4 and up and you can play with it today. You can even chat with the developers in the SignalR chat app in the 'aspnet' room at http://chatapp.apphb.com. Just /nick yourself then /join aspnet.

No monkeys were hurt in the writing of this blog post.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. I am 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 ORCS Web
Page 1 of 2 in the SignalR category Next Page

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