Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Use of SignalR for Real-Time Messaging on Windows RT Devices

$
0
0

Introduction

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 of it requires push (two-way communications, like duplex channel) 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.).

About SignalR

SignalR is a library in ASP.NET framework (starting from 4.5) and simplifies real-time communication approach directionally (server-to-client or client-to-server). 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

 

Link to SignalR Server API

Link to SignalR Client API

Link to SignalR Hub API

Solution

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.

Project structure

  • 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’

clip_image001[4]

Figure#1: POC solution structure

How it works

clip_image003[4]

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.

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)

await _hubConnection.Start();

  • 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).

clip_image005[4]

Figure#3: HubConnection (Client) class members

clip_image007[4]

Figure#4: Hub (Server) class members

Conclusion

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 for developing modern applications wherever you need to have push-based functionality. Big thanks goes to the SignalR team, well done in a short time!

References


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>