SignalR
http://c.statcounter.com/8888336/0/a10a53fb/1/SignalR makes it easy to build multi-user, real-time web applications with ASP.NET. SignalR establishes a persistent bidirectional connection which enables the server to call client methods or send messages to the client. This article gives a brief overview of SignalR. A commented link list helps getting started. This article is also a landing page for SignalR.
Overview
Concept
SignalR provides a bidirectional communication channel between (web) clients and a server side (web) application. SignalR is not limited to web application. A client can be every application or web page which uses JavaScript or .NET Framework 4.5 (or 4 with some restrictions) - like Windows Phone apps, SilverLight or Console applications. A SignalR server application has to be build with .NET Framework 4.5 (or 4 with some restrictions).
SignalR's communication channel is
- Bidirectional
Client and server can exchange messages or invoke remote methods. I.e. the communication is not limited to the request-response cycle. - Persistent
The connection is established once and used until the application terminates. Persistent connections can reduce the overhead per call significantly.
SignalR supports four communication technologies on the transport layer. Client and Server negotiate the best communication technology – depending on their capabilities – when they establish their connection. SignalR uses the fallback strategy shown in figure 1.
SignalR supports these communication technologies:
WebSockets
WebSockets are the best match. WebSockets (and also Server Sent Events) are standardized by the W3C as a part of HTML 5. The .NET Framework supports WebSockets since version 4.5.Server Sent Events
Server sent events offer a server-to-client push notification using DOM events.Forever Frames
The client embeds a invisible iframe. The "contents" send by server using the chunked encoding feature of the HTTP protocol. I.e. the response header does not contain a fixed length, and the response can be send in chunks - whenever the server has to notify the client. This approach establishes a long living connection.Long Polling
The client sends a (synchronous) request, but the server delays his response until the server needs to push information to the client.
SignalR has a layered communication architecture on client and server side as shown in figure 2:
Figure 2: Communication Layers
The (internal) Transport Layer handles the different communication technologies. The Persistent Connection API is independent of the underlying technology. It offers a message based API, which allows exchanging data between client and server (and vice versa). The Hub API is the next abstraction level. It enables client and server to invoke remote methods (instead of exchanging mere data).
SignalR exposes two communication levels possible:
- Persistent Connection API - low level, message-oriented communication
- Hub API - allows direct call of client and server methods
The Hub API offers the most convenient and preferred communication API. The Persistent Connection API might only be interesting, if you need to integrate with an existing message based server application or if you need for some reason detailed control on the message level.
Practical Scenarios
- Chat application
- Multiuser online game
- Application Monitoring
- Collaborative Work
Framework
SignalR was first implemented by Damian Edwards and … . It is available as open source.
The SignalR Framework consists of a set of .NET packages and one JavaScript package. The packages are offered via Nuget. Table 1 shows all packages and the (sub) packages they include.
Category
Package
Microsoft.AspNet.
Description
S
a
m
p
l
e
S
i
g
n
a
l
R
J
s
C
l
i
e
n
t
S
y
s
t
e
m
W
e
b
O
w
i
n
R
e
d
i
s
S
e
r
v
i
c
e
B
u
s
C
o
r
e
Included packages
Sample
A stock ticker sample
x
x
x
x
x
x
Meta Package
Meta package for web apps on IIS & ASP.NET
x
x
x
x
x
Clients
JavaScript client API
x
.NET client API
x
Host
Meta package for ASP.NET applications
x
x
x
OWIN host for SignalR
x
x
Scaleout Buses
Redis scaleout for SignalR
x
x
Service bus scaleout for SignalR
x
x
Core
Server side component
x
Table 1: SignalR packages
Typical usage scenarios are:
- Microsoft.AspNet.SignalR
is used by Web applications. It's a meta package which contains the most relevant packages for the server side.It also includes the Java Script client API, which is used by the delivered web pages. - SignalR.Client
is used by CLR-based client applications like Windows Phone, WPF or console apps.
Evaluation
Pros
- bidirectional communication for (web) clients
- automatically selects the best available communication mechanism
- it’s not restricted to web clients
Cons
- Not a reliable communication
Don’t use it if you need a guaranteed delivery of your messages. Bank Transaction are a negative example. - It uses dynamics. Some syntactical errors can only be detected at runtime. However, only a few lines of code are effected. So, it’s not a real problem detected all errors related to the usage of dynamics.
Link Collection
Videos
SignalR and WebSockets (6 min)
Scott Hanselman explains the key concepts of SignalR and WebSockets. An echo application and a stock ticker application are used as demos.Introduction to SignalR - Creating a Cross-Platform game (Screencast on Youtube by Filip Ekberg (MVP), 76 min)
Starts with an introduction of SignalR and explains how to creates a cross-platform game for different browsers and even Windows 8 Store applications.
Overview Articles
- Asynchronous scalable web applications with real-time persistent long-running connections with SignalR (Blog post, Scott Hanselman, August 2011)
Scott starts with the fact "Polling is a common example of hammering a screw." and give a brief introduction to SignalR. He explains the difference between low level connections and high level hub connections.
Official SignalR pages
SignalR (official web site)
The official web site of the SignalR project. It also links to the SignalR documentation on GitHub.SignalR Documentation (GitHub)
SignalR (asp.net)
Tutorials, Samples, Demos
Build a Progress Bar with SignalR (MSDN Magazine, Dino Esposito, March 2012 Issue)
This MSDN article starts with the popular chat sample. In addition it explains how progress bar can be implemented using SignalR. The code sample can be downloaded.Implementing Pub/Sub Applications with SignalR (MSDN Magazine, Bruno Terkaly, April 2012 Issue)
The article starts with a short examination of the polling problem and how it is solved using SignalR. The popular "Move Shape" is used as a demo application. He uses the popular chat sample and explains the 12 core lines of the client and server part.SignalR team on Jabbr (Forum)
Applications Using SignalR
- Jabbr
A support forum for SignalR which is implemented using SignalR. - ShooterR Game
A multi-user online game with spaceships. You can either play the game or download the source.
Misc
Automatic camel casing of properties with SignalR hubs (MSDN Blog, Stuart Leeks, September 2012)
Stuart writes: *"By default, SignalR preserves the case of property names when sending objects from the server to the client. My preference is for PascalCase on the server and camelCase on the client, and in this post I’ll show how you can achieve that."Running SignalR on Mono (TechNet article by Filip Ekberg)
The article explains the preparation of the SignalR libraries for Mono: compile SignalR dev branch and upload it to the server running Mono and Apache. A small sample application is used to the the installation.
See Also
Running SignalR on Mono
"You just need to compile the SignalR dev branch and use those libraries. Upload to a host that already runs Mono and Apache!"SignalR: Very Simple Example
This article is a slight variation of the "Move Shape" sample of the Microsoft Virtual Academy course "Building apps with ASP.NET 4.5".
User Page: Carsten Siemens
Information about other TechNet Wiki articles written by Carsten Siemens.