2. HOW WE DID IT IN THE PAST
We used to implement a polling system
Client Server
Got new Data ?
Got new Data ?
Got new Data ?
Got new Data ?
Here is new data
Got new Data ?
Got new Data ?
Got new Data ?
Got new Data ?
3. WHAT IS SIGNALR ?
ASP.NET SignalR is
•Library that makes developping real-time web functionality easy
•Allows bi-directional communication between server and client
•Server can push content to connected clients instantly
•Supports websockets and falls back to other compatible techniques for older
browsers
4. SQL DEPENDENCY
• A sql dependency object can be associated with a sql command in order to detect
when query results differ from those originally retrieved.
• Assign a delegate to the OnChange event, which will fire when the results change
for an associated command.
5.
6. LIMITATIONS FOR SQL DEPENDENCY
Query notifications are supported for SELECT statements that meet the following requirements:
•The projected columns in the SELECT statement must be explicitly stated, and table names must be qualified with two-
part names. Notice that this means that all tables referenced in the statement must be in the same database.
•The statement may not use the asterisk (*) or table_name.* syntax to specify columns.
•The statement may not use unnamed columns or duplicate column names.
•The projected columns in the SELECT statement may not contain aggregate expressions unless the statement uses a
GROUP BY expression. When a GROUP BY expression is provided, the select list may contain the aggregate functions
COUNT_BIG() or SUM(). However, SUM() may not be specified for a nullable column. The statement may not specify
HAVING, UBE, or ROLLUP.
•The statement must not reference a view.
•The statement must not contain any of the following: DISTINCT, COMPUTE or COMPUTE BY, or INTO.
•The statement must not reference tables or views from other databases or servers.
•The statement must not contain subqueries, outer joins, or self-joins.
•The statement must not use any of the following aggregate functions: AVG, COUNT(*), MAX, MIN, STDEV, STDEVP, VAR,
or VARP.
•The statement must not contain conditional statements that cannot change and cannot return results (for example,
WHERE 1=0).
…
7. DEMO ARCHITECTURE : FLIGHT INFO
ASP.Net
webserver
SignalRConnection
Console
application,
hooked on SQL
depdency
Sql Dependency
Flight Info
Administrati
on
SignalRConnection
1
2
3
Client
watching
Flight Info
SignalRConnection
4
Client
watching
Flight Info
4
Editor's Notes
Easy to use technique for Server - Client communication
In combination with SQL Dependency
Initiate a SqlDependency connection to the server.
Create SqlConnection and SqlCommand objects to connect to the server and define a Transact-SQL statement.
Create a new SqlDependency object, or use an existing one, and bind it to the SqlCommand object. Internally, this creates aSqlNotificationRequest object and binds it to the command object as needed. This notification request contains an internal identifier that uniquely identifies this SqlDependency object. It also starts the client listener if it is not already active.
Subscribe an event handler to the OnChange event of the SqlDependency object.
Execute the command using any of the Execute methods of the SqlCommand object. Because the command is bound to the notification object, the server recognizes that it must generate a notification, and the queue information will point to the dependencies queue.
Stop the SqlDependency connection to the server.
Web project:
Add nuget package Microsoft.AspNet.SignalR
Register the middleware in the startup class
Create a Hub class : FligtInfoHub with method UpdateFlightInfoBoard that the console can call
I this method a property is found Clients which exposes a dynamic property All on which you can call a method that will be executed on the client side javascript.
Javascript Client side
SignalR will generate script files on the fly which contain all the methods that will be called by the server
On document ready, create a hub on the signalR connection
Hook up a function on the hub.Client.updateFlightboardInfo to update the flight info.
At the end you need to start the hub, this way the connection is set up between client and server.
Console
On the trigger of the sql dependency create a HubConnection
Create a Hub for this connection with the name of the hub
Then start the connection and invoke the method.
Demo with breakpoints,
Demo without breakpoints