Using SignalR in LightSwitch to process toast notifications to connected clients.


In my previous post, I described a prototype for using signalR as a much simplified (and more powerful) replacement of the command table pattern.

In this post I will further elaborate on another nice feature of SignalR which are perfectly suitable for LightSwitch: the robust SignalR publish/subscribe mechanism for processing toast notifications to connected clients.

If you want to following things in code, it’s best to start with the code base of my previous post. We’ll work out an simple example: when a new customer is added to the database, we will send a toast notification to all connected silverlight clients:

How will we generate toast.

First problem to solve, how will we generate toasts?  The answer is very simple. Go and grab Mr. Yossu’s excellent LightSwitch Extension: Pixata custom controls for Lightswitch 

Note that toasts will only work in out-of-brower mode !

Server side handling

The amount of code necessary server side is really scary:

public partial class ApplicationDataService
        partial void Customers_Inserted(Customer entity)
            var myHub = GlobalHost.ConnectionManager.GetHubContext<MyHub>();

            var result = myHub.Clients.CustomersInserted(entity.LastName, this.Application.User.FullName);

So, the server is here really calling all Clients. More precisely, the server will call the “CustomerInserted method.

Client side handling

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using SignalR.Client.Hubs;
using PixataCustomControls.Presentation.Controls;
using Microsoft.LightSwitch.Security;
namespace LightSwitchApplication
    public partial class Application
        public IHubProxy HubProxy { get; set; }
        partial void Application_Initialize()
           HubConnection hubConnection;

          hubConnection = new HubConnection("http://localhost:8010"); //make sure it matches your port in development

            HubProxy = hubConnection.CreateProxy("MyHub");

            HubProxy.On<string, string>("CustomersInserted", (r, user) =>
                this.Details.Dispatcher.BeginInvoke(() =>
                    PixataToastHelper.ShowToast("New Customer inserted by " + user, "Customer Name : " + r);


Setting up notifications is terribly simple with SignalR.

Consult the SignalR documentation for sending the notification to the sender only or to a group of users.