Consuming Server-Side SignalR Events in Universal Windows App Clients

In a previous article I wrote about creating server side SignalR timer events.

As part of my learning of SignalR I wanted to see how easy it would be to create a Universal Windows app consuming these server side events.

So first off I created a new blank Universal app:

creating universal app in Visual Studio screenshot


Next installed the SignalR NuGet package into both app projects: install-package Microsoft.AspNet.SignalR.Client

In the Windows 8.1 Store app project XAML I added a simple bound TextBlock that will display the server messages:


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
            <TextBlock Text="{Binding Uptime}">please wait...</TextBlock>

I also added similar XAML in the Windows Phone 8.1 main page.

In the code behind for both main pages I created a data context from a viewmodel class that is defined in the UpTimeUni.Shared project. This means it will be available from both apps.

public MainPage()

    DataContext = new MainViewModel();                

Next the viewmodel class looks like the following:

using System;
using System.Diagnostics;
using Windows.UI.Core;
using Microsoft.AspNet.SignalR.Client;
using Microsoft.AspNet.SignalR.Client.Transports;
using PropertyChanged;

namespace UpTimeUni
    internal class MainViewModel
        private readonly CoreDispatcher _dispatcher;

        public MainViewModel()
            _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

            var connection = new HubConnection("");

            var hubProxy = connection.CreateHubProxy("UptimeHub");
            hubProxy.On<string>("internetUpTime", UpdateTime);


        public string Uptime { get; set; }

        private async void UpdateTime(string s)
            await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => Uptime = s);

Here I’m using Fody to automatically implement all the INotifyPropertyChanged boilerplate code, hence the [ImplementPropertyChanged] attribute. This relies on installing the PropertyChanged.Fody NuGet package into both the app projects.

Now running the Windows Store app we can see the app updates with the Internet uptimes every second.

Windows 8.1 Store app running against SignalR server


So next I changed the startup project to the phone app and running showed just a blank screen.

At first I thought I had messed up the XAML binding but this was fine. I put a breakpoint in the viewmodel’s UpdateTime method but it never got hit, it was never getting called from SignalR.

I added some SignalR tracing code (based on this) to the viewmodel constructor to see what was going on:

public MainViewModel()
    _dispatcher = CoreWindow.GetForCurrentThread().Dispatcher;

    var connection = new HubConnection("");

    // Add some tracing help
    connection.TraceLevel = TraceLevels.All;
    connection.Closed += () => Debug.WriteLine("[Closed]");
    connection.ConnectionSlow += () => Debug.WriteLine("[ConnectionSlow]");
    connection.Error += (error) => Debug.WriteLine("[Error] {0}", error.ToString());
    connection.Received += (payload) => Debug.WriteLine("[Received] {0}", payload);
    connection.Reconnected += () => Debug.WriteLine("[Reconnected]");
    connection.Reconnecting += () => Debug.WriteLine("[Reconnecting]");
    connection.StateChanged +=
        (change) => Debug.WriteLine("[StateChanged] {0} {1}", change.OldState, change.NewState);

    var hubProxy = connection.CreateHubProxy("UptimeHub");
    hubProxy.On<string>("internetUpTime", UpdateTime);


And now running the phone app I could see debug messages such as:

[StateChanged] Disconnected Connecting
[StateChanged] Connecting Connected
[StateChanged] Connected Reconnecting
[StateChanged] Reconnecting Connected

After some searching online I found a Stack Overflow comment that suggested you need to configure SignalR to use long polling when running in the emulator. I don’t have a Windows Phone 8.1 device to check this on outside of the emulator at present.

So I added the following code to configure the phone app to use long polling:

            // Doesn't work (just?) in emulator with default
            connection.Start(new LongPollingTransport()); 

Running the phone app now works as expected.

SignalR running in WindowsPhone emulator

Once I update my phone to 8.1 I can see if this behaviour is just an emulator quirk.

If you strip away all the additional stuff, it’s effectively just 4 lines of code to receive SignalR messages, which is pretty amazing.

Pingbacks and trackbacks (3)+

Add comment