Note: This sample is targeting 1.0RC1, I will update the post after RTM is released
SignalR offers a simple and clean API to write real time web applications where the server needs to continuously push data to clients. Common applications are chat, news feed, notifications, multiplayer games.
In this sample, I demonstrate powerful features like:
- A server implementation hosted in IISExpress
- Client implementations running on IISExpress, a Console Application, and a Windows Store App
- Doing request/response operations sync and async
- Server pushing broadcast messages to ALL clients
- Server pushing group messages to specific devices like a web browser, a desktop, or a tablet.
Server
Server derives the Hub class, and it handles incoming requests in two ways:
- Method "y Request(x)" transparently sends a response to client
- Method "void RequestAsync(x)" uses Clients.Caller.<DynamicMethod> to send a response to client
Server is also pushing data to clients in two ways:
- hubContext.Clients.All.<DynamicMethod>
- hubContext.Clients.Group(groupName).<DynamicMethod>
As you can see, dynamic methods define the events that need to be handled on the client side. Think of them as if they were interface methods to execute remote calls.
namespace SignalR.Sample { public class SampleHub : Hub { public static void Start() { ThreadPool.QueueUserWorkItem(_ => { IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext<SampleHub>(); … |
Client
To write a client, you must define how to handle Server <DynamicMethods>: "client.Broadcast", "client.Group", and "client.ResponseAsync".
Then, client is started, and after the connection succeeds, the client can make requests to server by invoking "server.requestAsync" and "server.joinGroup".
You will see similar patterns in the ConsoleApp and WindowsStoreApp clients.
$(function () { $.connection.sampleHub.client.Broadcast = function(value) { $('#BroadcastTextArea').html('<pre>Broadcast: ' + value.Now + ' ' + value.Integer + |
There is more information about how I wrote the code when running the sample, simply follow these instructions:
- Download and extract SignalR.Sample.zip
- On Visual Studio 2012, open SignalR.Sample.sln
- Right-click the solution and select "Set Startup Projects…"
- Select "Multiple startup projects", and set the projects in this order:
- SignalR.Sample
- SignalR.Sample.Client
- SignalR.Sample.WinRTClient
There are more advanced samples in the SignalR source code repository, follow these steps to get them:
- Install the latest Git for Windows
- Open a Command Prompt, create a folder, and download source code
md C:\SignalR\release cd C:\SignalR\release git clone -b release https://github.com/SignalR/SignalR.git . |
- On Visual Studio 2012, open C:\SignalR\release\Microsoft.AspNet.SignalR.sln
SignalR has recently shipped as RC, more details here. For the RTM version, I want to post a more complex sample, my idea is to demonstrate how to publish the server implementation on Azure and use scale-out with Service Bus. Please post a comment if you are interested to see something else.