Click here to Skip to main content
15,888,521 members
Articles / Programming Languages / C#

SignalR - Group Notifications

Rate me:
Please Sign up or sign in to vote.
4.60/5 (22 votes)
15 Jun 2012CPOL3 min read 133.1K   9.8K   76   28
A Sample Broadcast Notification System with Groups from an External Application

Introduction

The aim of what we will be trying to accomplish in this tutorial is to create a sample Broadcast Notification System using SignalR where specific groups can receive "notifications" in real time, from an external application.

Background

A Real World Application: A perfect case for this would be a single source of code that services multiple customers or domains. Example; http://twitter.com/{username}, and based on the parameter username, the site is customized to include the content of that user. Single code source, but multi-domain.

Aim Simply to notify (send messages), based on a Group or Unique Identifier. Secondly to do this using an external application, that way a separate Windows Service (for example) can be created to broadcast messages to Groups. 

Design: A simple messaging architecture based on three classes;

  1. Message - a basic unit
  2. Messenger -  a manager of Messages (actions to take on messages) 
  3. MessengerHub - the interface for SignalR 

Using the code  

(A) The Server-Side

First design the basic types that will be used on the Server-Side. When I say Server-side, I mean in this case the actual WebSite/Web Application or Project. In this sample code, I will be using a WebSite Project in Visual Studio 2010.

1. Message

Image 1

Quite simply the container or basic data unit that represents an actual Message  

2. Messenger

Image 2

In the Messenger class we have a created an Instance manager which will be dynamically generated on Request (using the type Lazy) 

Secondly a GetAllMessages will be used for getting all the messages that is contained in the ConcurrentDictionary. You can better filter this for support for your groups etc.

Thirdly, a BroadCast to a specific group which will the foundation of all our Messenger's (so to speak); where it will get all clients by a specific group.

Here is the GetClients method:

Image 3

This will basically find our MessengerHub, and return a set of clients that belong to the specified group. Then using the BroadCast message above, using the "add" method to send a notification to each one of the Clients in that group. 

3. MessengerHub

Image 4

We create an AddToGroup, GetAllMessages, BroadCastMessage to our class which will be our implementation of Messengers, but will also be the exposed interfaces for our remote application to call, when it connects to x.x.x.x/signalr/hubs. 

(B) Client-Side Script

Image 5

This client side will basically the "start" of all things SignalR. Please note in the WebSite Project I have created, the previous three classes were added into "App_Code".

The above is quite simplistic. jGrowl is being used for the notification system or pop-up that will appear in browser.

The group that I am using ("sourcing") can be custom to whatever suits you (customer name, hostname etc). 

(C) The External-Application

Image 6

The Console Application above is basically the external application and the method by which SignalR is remotely called. Basically it creates a Proxy which communicates to the remote site.  

The result:  

Image 7

Points of Interest 

In my case above; http://localhost:9616/signalr. It's also a great test to check if your SignalR is working on the Server-Side; http://localhost:9616/signalr/hubs. You should see an extension, with all your "methods" you exposed. 

Both samples are included and should get you started.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Senwes
South Africa South Africa
Hacking at code.....

Comments and Discussions

 
GeneralMy vote of 3 Pin
Vitaly Tomilov15-Jun-12 7:41
Vitaly Tomilov15-Jun-12 7:41 
GeneralRe: My vote of 3 Pin
andrusha00715-Jun-12 11:11
andrusha00715-Jun-12 11:11 
GeneralRe: My vote of 3 Pin
Vitaly Tomilov15-Jun-12 13:57
Vitaly Tomilov15-Jun-12 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.