About Links Archives Search Feed Albums of Note

DDD Southwest Session Notes 2 : Web Sockets & SignalR

The second session of my day at DDD Southwest [http://blog.hmobius.com/post/2012/05/29/DDD-Southwest.aspx] was a talk by Chris Alcock of The Morning Brew [http://themorningbrew.net/] about Web Sockets [http://dev.w3.org/html5/websockets/], a new web standard for real-time communications between a web server and a page on a client browser, and SignalR [http://signalr.net/], 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 [http://www.developerfusion.com/article/143158/an-introduction-to-websockets/] 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’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.

How do they work?

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

Web Socket Server Support * Many implementations are available [http://en.wikipedia.org/wiki/Comparison_of_WebSocket_implementations] * 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 [http://nuget.org/packages/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 [http://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 [http://signalr.net/] is an async library for .net to help build real-time, multi-user interactive web apps

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

It also supports a more interesting Hub API

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.

Posted on May 29, 2012   #Attending Events     #Geek Stuff  

← Next post    ·    Previous post →