Dan Maharry

DDD Southwest Session Notes 2 : Web Sockets & SignalR

by

The second session of my day at DDD Southwest was a talk by Chris Alcock of The Morning Brew about Web Sockets, a new web standard for real-time communications between a web server and a page on a client browser, and SignalR, an open source library that make use of web sockets.

N.B. The section of Chris’ talk on Web Sockets and how they work is better fleshed out here on DeveloperFusion than the notes below.

Q: What do we mean by real-time web applications?
A: Time-dependent applications such as the stock market and weather.

Q: What do we mean by interactive web applications?
A: Applications such as chat and auctions (which are also real-time) which work only with users communicating with each other.

Q: How do we handle these communications?
A: Currently we use periodic refreshes from the browser client to update what a user sees but it takes place at the page's\user’s discretion which is a bit rubbish. Long polling works a bit better. Flash is also a possibility.

Web Sockets? What? When? How?

A new option in real-time web communication has appeared called Web Sockets. A Web Socket connects two computers - client and server.

  • It is a web standard – RFC6455
  • W3C defines the WebSocket API as a draft standard and part of the wider HTML5 standard.
  • Currently there is limited support for it. The RFC version (there were 12 pervious versions) is only supported in a few recent browser versions. IE10, FF11, Chrome16, Opera

How do they work?

  • Over normal web requests, so firewall friendly. Port 80, 443 using ws:\\ and wss:\\ respectively.
  • Request includes the upgrade header to notify this is a web socket request.
  • Coms are two-way a la traditional sockets & can stream data.
  • Supports cross-domain requests.

After the initial handshake, the socket is left open for communication. Your proxy server will need to understand this.

Web Socket Client API

The Web sockets API has a straightforward set of functions

  • new WebSocket(url)
  • onopen
  • onclose
  • onerror
  • onmessage
  • close
  • send
  • readystate (enum - Connecting, Open, Closing, Closed)

Web Socket Server Support

  • Many implementations are available
  • For .net developers, web sockets are only supported in IIS8 on Windows 8 (they require lots of changes to HTTP stack). NB IIS8 Express on Windows 8 does not support it.
  • .net 4.5 adds two new important members to the HttpContext class
    • HttpContext.IsWebSocketRequest
    • HttpContext.AcceptWebSocket
  • nuGet Microsoft.WebSockets package for ease of development.

When to use Web Sockets...

  • Only modern browsers support Web Sockets as a client API. “Polyfills for ws” such as socket.io do exist however.
  • Windows 8 is required to act as a web sockets server.
  • You won’t be able to host a Web Socket server in the Azure cloud until after Windows 8 has been deployed to the cloud.
  • Performance is good though - you control the messages sent.

SignalR. What? When? How?

SignalR is an async library for .net to help build real-time, multi-user interactive web apps

  • on github, v0.5
  • It is not an official MS project. Damian Edwards and David Fowler team leads (both from ASP.NET team)
  • Available on nuGet
  • Runs on Mono
  • Requires .net 4.0+
  • Uses Dynamic typing, TPL, jQuery and more.
  • Two types of connection (persistent like web sockets and hub)
  • Supports multiple methods of connecting (long polling, websockets, forever frame (IE only), server sent events
  • More than just one connection (seamless)

SignalR supports persistent connections with a similar API to WebSockets (see docs on github)

It also supports a more interesting Hub API

  • The Hub API is a RPC-like implementation.
  • It allows you to make method calls from server on the client and vice versa
  • It allows you to share variables between the two
  • Server implementation uses hub base class
  • Dynamic types are used for proxy
  • SignalR Clients vary how API is presented.

Demo

  • Methods are dynamic so intellisense is not available.
  • SignalR requires two scripts - jquery.signalr.js is the base library. signalr/hubs/ is where we implement the hub connections.
  • SignalR deals with calling pascal-cased methods on the server (e.g. Reflect) with camel-cased methods on the client (e.g. reflect)
  • Don't use a folder called signalr in you own app as SignalR reserves that folder for its own use and things won't work.

Notes on Implementing A Hub

  • You must wire up your client side code before connecting .
  • A new hub instance is required on each request so you can't store hub member variables to store state.
  • Transports can timeout and thus disconnect - makes for odd debugging experiences
  • Server has lots of concurrent connections so it will need optimizing. See github docs for a guide.

There are several SignalR clients - five are included with SignalR and there are several third party ones.

SignalR Hosting Options

  • Win/Mono
  • ASP.NET, Self Host, OWIN
  • It can scale out to Webfarms (Redis, Azure Queues)

WebSockets is an available transport for SignalR, but only on Win8 for reasons noted earlier. Also note that currently SignalR on Web Sockets is broken in the v0.5 build but there is a workaround available on the github site.

The slides for this presentation are available at  http://www.cwa.me.uk/?page_id=68.

DDD Southwest Session Notes 1 : Performance & Scalability

by

The first session of my day at DDD Southwest was a talk by Marc Gravell of Stack Overflow about how they approach performance and scalability issues. As with any talk specific to a work place or specific site, your mileage may vary but the two tools he demonstrated could certainly be used anywhere.


Performance Myth #1: Adding a server will make your site faster. This is NOT TRUE. If you add a server to your web farm or cluster, your site will not get served any faster. Websites do not trouble the CPU very much. Indeed, StackOverflow runs at 10% CPU load give or take. That's for a website serving ~7 million page views a day according to Quantcast.

Profiling

When a performance-related issue is logged on a site, sometimes it is possible to use a coarse-grained event log or profiling tool to find and diagnose the issue. Most of the time, this is not possible.

Of course, the finer-grained the level of profiling on your live site, the greater the overheads and the slower the actual site. Beware net admins with guns. Especially NRA members (such at those at StackOverflow :-)

One tool written by the StackOverflow team to address this issue is called (MVC) MiniProfiler, a very lightweight, almost zero friction profiler for websites.

Install it into your site using nuGet

PM> Install-Package miniprofiler

This adds two new references and two new files to your web project. Simply uncomment the call to RenderIncludes in your layout.cshtml page to have MiniProfiler start working. On each page, MP throws in basic profile info in top left. Clicking on the MP tab will display the time for each function to complete before the rendering of the page.

MiniProfiler appears in the top left fo your browser window

MiniProfiler can also monitor database requests by wrapping your DbConnection object or DbContext object if your site uses Linq2SQL or the Entity Framework.

DbConn conn = new SqlConn(...); 
conn = new ProfiledDbConnection(conn, MiniProfiler......); 
// do something with profiled connection

When run MP now also displays the number of SQL commands sent to db and what they were (formatted nicely). It will also alert you if a page is running duplicate commands or other (n+1) scenarios which you can go back to improve. Look for the ! in the MP smart tag which will appear if this is happening. MP also has a share button (provider-based) for passing the profiling info to others.

Access to MP can be configured in App_Start/Miniprofiler.cs. By default, MiniProfiler appears onscreen only if the page request is to localhost but you can change that to something role-based, page-based etc.

  • Q\A : MP should work with any MVC ViewEngine as it just wraps the ViewEngines much like the DB connections. See A-S/MP.cs for code.
  • Q\A : MP can't really account for the 'works on my machine' / 'fails on production environment' scenarios
  • Q\A : MP also works with an AJAX update. MP adds 
    extra timings for the async call down the left hand side fo the screen.
  • Q\Ans: You can use the DBProfiling side of it without the Web stuff. Ask the prfiling object to get the db data and then work with it.
  • Q\A : MP would work with webservices and CMD apps but you need to think about how and when MP would show its info.
  • Q\A : MP doesn't currently support async multi-threaded stuff....

Introducing Dapper, A Read-Focused Data Helper class

StackOverflow was originally built using LINQ2SQL in .NET 3.5 When .NET v4.0 came out, LINQ2SQL appeared to stall reasonably frequently. A database request would suddenly take 400ms and not 4ms. The LINQ library wasn’t open source, so SO had to diagnose and then figure out how to go around the problem?

  • Tried their own sql generation trees
  • Tried just executing raw SQL rather than letting L2S generate it. That didn't work either.

In the end they wrote their own data access stack called Dapper. Heavily favouring reads. 1in500-ish commands in Stack Overflow are updates. The rest are reads. Dapper has a very similar syntax to LINQ Context queries but uses anon objects to pass parameters to queries. The speed increases in Dapper are the improvements in the materialiser (getting the field names etc and pushing them into the object) which appears to have been the issue in LINQ2SQL v4

  • Q/A : Dapper objects are not connected to the LINQ context object so calling SubmitChanges doesn't work. SO still uses L2S for writes mostly (although Dapper is starting to do writes also).
  • Q/A : Dapper wraps generic Connection objects. So it should work over any .net conn object as long as the syntax for that particular DB is covered (diff plsql syntax over t-sql etc)
  • Q/A : Dapper source includes perf tests against several other ORMs (run in release mode)

StackOverflow uses Redis for caching. You could also use it for session state etc. Redis is a hi-perf key-value store.

In conclusion, Marc offered some simple rules for improving the performance of your data access routines:

  • Learn SQL - dont rely on LINQ
  • Keep it simple
  • Cache , cache,cache
  • Investigate other tools – other ORMs, write your own? e.g. a noSQL store for Caching, Session such as Redis
  • Check the serialization - how much is serialized? Can it be reduced etc.

DDD Southwest

by

Saturday May 26 saw nearly 300 developers from around the UK converge upon The University of West England, Bristol for DDD Southwest v4. As with all DDD events, the schedule had previously been voted for by those attending and those wishing to attend, and, of course, by speakers looking for that nice new black polo shirt emblazoned with DDDSW's three cow logo.

DDDSouthWest4_Logo

Blessed with a fantastic sunny day, a cooling breeze, awesome food (pasties and cream teas at a dev event? Rock on!) and five sessions of techy goodness, it was a great event for learning, networking, catching up, winning swag, hunting for plug sockets under tables and getting burnt by the sun. I think we all slightly wished the sessions could have been moved outside - the  air conditioning was a little lacking in some rooms - but with UWE's free wifi a doddle to set up and some truly entertaining speakers, we barely noticed the time before it was all over for another year.

I attended and took notes on four introductory sessions which I've written up and annotated for all to read.

Hooray for DDD Southwest then - the DDD event with good sun and better food. Other people thought so too. Props to Gary Park for building this list of other DDDSW roundups and slides.

Up next on September 1 is DDD10 at Reading. The Call for Speakers has already gone out so if you fancy running a session, propose your talk right now.

Looking for .NET at Silicon Milkroundabout

by

Silicon Roundabout is the nickname for the Old Street/Shoreditch area of London which now houses some 150+ tech startups and businesses at some way through various venture capital funding cycles. Songkick, who were one of the first companies to set up in the area, also set up the Silicon Milkroundabout, a jobs fair for graduates, developers, project managers, UI designers, and engineers to meet and greet the startups with positions to fill. The third of these events took place this past weekend, 26-27 May, in Brick Lane. 130 companies exhibiting and 800 jobs to fill. I ventured forth to see if .NET played any part in this oasis of greenfield development.

In short, the answer was no. Of the 130 companies in the fair directory, only 7 listed .net development of any kind in their vacancies. 

About half those exhibiting were looking for iOS, Android, and HTML5+Javascript mobile developers while the rest were split between Java, Ruby and a little Python (Django) and Perl. ‘Big data’ developer positions were also quite popular.

A brief discussion with some companies indicated that this skew to open source platforms was the cheapness to set up before VC funding allowed them the luxury of some co-located rackspace. it didn’t seem to matter that with the advent of the Visual Studio Express editions, MonoDevelop and even SharpDevelop, .NET is as free as any other platform.

Perhaps surprisingly for a tech fair, those with paper printouts of their attendance confirmation  emails were fast-tracked to the registration desk while those with digital copies on phones or iPads had to wait outside. Perhaps next time round they can send QR codes to attendees so all they have to do is scan them to confirm their attendance.

Changes in Windows Azure Service Names

by

Just a quick note here to keep a track of what everything is being called and what it was. It’s all quite nebulous is the cloud, innit?

Prior Service Name

New Service Name

Windows Azure Compute

Cloud Services

Windows Azure Platform - All Services

All Services

Windows Azure CDN

CDN

Windows Azure Storage

Storage

Windows Azure Traffic Manager

Traffic Manager

Windows Azure Virtual Network

Virtual Network

AppFabric Cache

Cache

AppFabric Service Bus

Service Bus

AppFabric Access Control

Access Control

SQL Azure

SQL Database

SQL Azure Reporting Service

SQL Reporting