Especially in IoT, we never want to have our devices constantly polling for updates, rather we would greatly prefer to have the server push updates.
What is SignalR
SignalR is a useful framework from Microsoft that enables real-time communication between client and server. By using a range of different methods. There are many reasons use SignalR over other alternatives:
- Quick and easy to use
- Uses a Publisher subscriber approach
- Lets you leverage existing skills
- Integration with ASP Web applications and web-APIs is simple
Constantly Polling VS Publisher-subscriber
For demonstration imagine the following to conversations as an example of the traffic between a server and an IoT device that takes a reading on a request from the server.
“IoT device” “Server”
Constantly Polling Approach
- “Do you want me to take a reading?”
- “No thank you”
- “Do you want me to take a reading?”
- “No”
- “Do you want me to take a reading?”
- “NO”
- “Do you want me to take a reading?”
- “NO!!!!”
- “Do you want me to take a reading?”
- “Yes yes I want you to do a reading “
- “Here is the data from the reading you asked for!”
- “Do you want me to take a reading?”
Publisher-subscriber Approach
- “Can you please take a reading?”
- “Here is the reading you asked for!”
As demonstrated above the constantly polling approach consumes significantly more resources than the publisher-subscriber approach.
1. Create a new universal application
In this tutorial, the Windows 10 universal application I create will be called ‘SignalRPiExample’.
2. Add the following nuget packages
Run the following commands in the Package Manager Console:
- Install-Package Microsoft.AspNet.SignalR.Client
- Install-Package PropertyChanged.Fody
3. Update mainpage.xaml
First we will add a simple TextBlock with a binding to the page to display the messages from SignalR :
4. Create the viewmodel for the MainPage
To have our bindings to update automatically, we’ll have to implement INotifyPropertyChanged into our Universal application.
This process can be time-consuming and commonly lead to errors due to small oversights. Therefore, we will streamline this process by using Fody add our INotifyPropertyChanged boilerplate code into our properties at compile time.
5. Wiring it all up
Go to the code behind the Main page.xmal and add the MainViewModel.
6. Run
Please note this video has been editing to quickly demonstrate the end result, your deployments may take longer.
An error occurred!
If this is your first time deploying to the Raspberry Pi please read my post on ‘Getting Started’
SigalR: ‘System.IO.FileNotFoundException’ occurred in Microsoft.AspNet.SignalR.Client.dll
As of the time of writing this post, there is a known issue with SignalR and Windows Universal applications.
-
Download the Microsoft.AspNet.SignalR.Client.dll from this link.
-
Delete the following references Microsoft.AspNet.SignalR.Client and Microsoft.AspNet.SignalR.Client.Store.
-
Create a Binaries folder in your project
-
Add the Microsoft.AspNet.SignalR.Client.dll from the zip to the Binaries folder.
-
Now add a reference to the Microsoft.AspNet.SignalR.Client.dll in the Binaries folder
You many also need Json.NET to the latest release. Run the following command in the Package Manager Console:
- Update-package Newtonsoft.Json
Fody: ‘Unknown custom metadata item kind: 6’
As of the time of writing this post, there is a known issue with Fody version prior to 1.26.2 and Visual Studio 2015.
The solution is to update Fody to the latest release. Run the following command in the Package Manager Console:
- Update-package Fody
Be sure not to overwrite your weavers!
How do I set up the server side part of SignalR?
Although this topic falls outside of the scope of this post, I’ve had a few people ask me this question. Therefore, I can strong recommend watching the following video on the topic.
Feel free to tweet me comments, feedback or questions to @ChrisBriggsy.