One of the challenges using Windows RTs is to send messages from a store application deployed on one Windows RT tablet (Surface RT in my case) to the other application on another Windows RT tablet. It is a challenge, mainly because it requires push (two-way communications, like WCF duplex channel) messaging and limitations on Windows RT platform (can’t be domain-joined, no queues, you can’t deploy any Win-32 type application, app container etc.).
SignalR, developed by an MS team, is an API (part of ASP.NET framework, starting from .NET 4.5+) that provides simplified real-time and bi-directional communication. So, the relevant keywords would be:
- Real-time communication
- Bidirectional (send/receive)
- Enriched push services: It can broadcast the message to all, to one, or to all but one, etc.
- Hubs/Persistent connection
- Durable: It uses one of the following APIs (listed in order) for underline communication – that is available in the client:
- web sockets,
- server sent events,
- forever frames,
- long polling
The solution detailed here demonstrates a case scenario in which tablet users communicate each other by sending messages from the store applications deployed in their Windows RT devices.
- Device 1: A store application named “SignalR.ClientX” running on clientX tablet: It lets clientX to start a session with a group code and to send messages to all/one/group-by clients associated with the group name.
- Device 2: A store application named “SignalR.ClientY” running on clientY tablet(s). It lets clientYs to join to an existing session (created by the client) by providing group code and to send messages to participants.
- SignalR Server (Host):
- A cloud service application named “SignalR.Cloud” hosted on the Windows Azure.
- A web application named “Microsoft.AspNet.SignalR.Server.Web” deployed to cloud services. This is exactly same bits taken from SignalR site. Or get it from nuget package console in VS IDE by typing ‘Install-Package Microsoft.AspNet.SignalR.Sample’
Figure#1: POC solution structure
How it works
Figure#2: POC solution message flow
As you see, the solution combines Windows Azure cloud services representing SignalR server and 2 Windows store applications representing SignalR clients running on Surface RT devices. Unfortunately, I can’t cover all those things in the picture, so will focus on client API calls for now.
Here are the simplified steps:
- Create a hub connection
_hubConnection = new HubConnection(_url);
- Create a proxy from the connection with the name of the Hub (the name given in your server side code)
_hubProxy = _hubConnection.CreateHubProxy(_hubName);
- Start the connection (asynchronous Task)
- Execute a method on the server side hub (asynchronous Task).
var joinGroupResponse =
await _hubProxy.Invoke<string>("JoinGroup", _hubConnection.ConnectionId, groupName);
How to run it
- To run this project, you need to have a Windows Azure membership and a service bus (sb) namespace (not covered here, please visit here to start)
- Update the connection string named ‘Microsoft.ServiceBus.ConnectionString’ in the web.config file with your sb credentials
- Re-built the solution (check SignalR references, if not get the nuget packages )
- Deploy cloud service to your Windows Azure
- Deploy ClientX app to a Win RT tablet
- Deploy ClientY app to another Win RT tablet
- Run ClientX app, enter a session code then click ‘Start Session’ button
- Run ClientY app, enter the code then click ‘Join To Session’ button
From there, you can send messages from either app. All the messages passed/received along with their trace data are written to textbox (center panel).
Figure#3: HubConnection (Client) class members
Figure#4: Hub (Server) class members
Please note these:
- Use of SignalR is not limited to this scenario (Windows store application), and in fact, it’s is quite large (desktop, web, phone, etc.). Please see the links provided below for further details.
- The demonstrated solution is also applicable for offline session scenario. In this case, an on premise Windows Server Service Bus can be used for hosting SignalR server API.
- Source code is shared for further help.
- SignalR uses persistent and dedicated connections, so, you need to align your server connection capabilities with number of clients you provision.
In conclusion, you must consider SignalR API when developing modern applications wherever you need to have push-based functionality. Big thanks goes to the SignalR team, well done in a short time!
- SignalR Wiki: https://github.com/SignalR/SignalR/wiki
- ASP.NET SignalR Hubs API Guide (.NET Client C#): http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-net-client
- Github SignalR: https://github.com/SignalR/SignalR
- Service Bus for Windows Server Overview (On-Premise case): http://msdn.microsoft.com/en-us/library/windowsazure/jj193027(v=azure.10).aspx
- Windows Azure: http://www.windowsazure.com/en-us/documentation/?fb=en-us
- How to create a web role project: http://www.windowsazure.com/en-us/develop/net/tutorials/multi-tier-web-site/3-web-role/
- Push Technology: http://en.wikipedia.org/wiki/Push_technology