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;
- Message - a basic unit
- Messenger - a manager of Messages (actions to take on messages)
- 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
Quite simply the container or basic data unit that represents an actual Message
2. Messenger
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:
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
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
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
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:
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.