Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Web Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SignalR, Quartz.Net and ASP.Net: part 2 the webclientSignalR and VB.Net: Hubs and objects »
    comments

    Introduction

    I needed a way to let my users know when something new arrived. For this I need to check an external system. The only way to do this is to pole the existing system every so often. I could easily do this on the clients, but this would create a lot of traffic if you had lots of clients. I decided have my server do the polling and then let the clients know when something new has arrived.

    The best way I have found to this was to use Quartz.Net and SignalR.

    The server

    To start you can make an empty ASP.Net project.

    Normally you would make a service to something like this. But I kinda need my webserver for the SignalR part. But Quartz.Net can deal with this.

    First thing to do is to make our Quartz.Net Job.

    1. Option Strict Off
    2.  
    3. Imports Microsoft.AspNet.SignalR
    4. Imports Quartz
    5. Imports Dapper
    6.  
    7. Namespace Server
    8.  
    9.     Public Class NewCases
    10.         Implements IJob
    11.        
    12.         Private ReadOnly _context As Object
    13.  
    14.         Public Sub New()
    15.             _context = GlobalHost.ConnectionManager.GetHubContext(Of Server.BeCare)()
    16.         End Sub
    17.  
    18.         Public Sub Execute(context As IJobExecutionContext) Implements IJob.Execute
    19.             Try
    20.                 Using con = New SqlConnection("connectionstring")
    21.                     dim newCases = con.Query(Of String)("select * from newcases where status = 'new'").ToList
    22.                     If newCases.Count > 0 Then
    23.                       _context.Clients.All.newCases(newCases)
    24.                     End If
    25.                 End Using
    26.             Catch ex As Exception
    27.                 _context.Clients.All.newCases(New List(Of String) From {ex.Message})
    28.             End Try
    29.         End Sub
    30.  
    31.     End Class
    32. End Namespace

    I'm using dapper to get my data and I'm using the globalhost to get the context for SignalR so I can send a message to all my clients whenever there are new cases available.

    Now I just have to schedule this.

    I can do the configuration in code in the Global.asax

    1. Private Shared _scheduler As IScheduler
    2.  
    3.     Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    4.         RouteTable.Routes.MapHubs()
    5.  
    6.         Dim factory = New StdSchedulerFactory()
    7.         _scheduler = factory.GetScheduler()
    8.         Dim startTime = DateBuilder.NextGivenSecondDate(Nothing, 10)
    9.         Dim job = JobBuilder.Create(Of NewCases)().WithIdentity("job6", "group1").Build()
    10.  
    11.         Dim trigger As ISimpleTrigger = TriggerBuilder.Create().WithIdentity("trigger6", "group1").StartAt(startTime).WithSimpleSchedule(Function(x) x.WithIntervalInSeconds(5).RepeatForever()).Build()
    12.  
    13.         _scheduler.ScheduleJob(job, trigger)
    14.         _scheduler.Start()
    15.     End Sub

    SO I create a scheduler, then a job and a trigger and add those to the scheduler after which I can Start the scheduler. I made a trigger with an interval of 5 seconds here. And I made it start 10 seconds after our application starts.

    I also made a Hub so that our clients have something to connect to.

    1. Option Strict Off
    2.  
    3. Imports Microsoft.AspNet.SignalR.Hubs
    4. Imports System.Data.SqlClient
    5. Imports Dapper
    6.  
    7. Namespace Server
    8.     Public Class BeCare
    9.         Inherits Hub
    10.  
    11.         Public Sub Refresh()
    12.             Try
    13.                 Using con = New SqlConnection("connectionstring")
    14.                     Dim newCases = con.Query(Of String)("select * from newcases where status = 'new'").ToList
    15.                     If newCases.Count > 0 Then
    16.                         Clients.All.newCases(newCases)
    17.                     End If
    18.                 End Using
    19.             Catch ex As Exception
    20.                 Clients.All.newCases(New List(Of String) From {ex.Message})
    21.             End Try
    22.         End Sub
    23.        
    24.     End Class
    25. End Namespace


    The client

    I made a basic client to test if this works.

    1. Dim connection = New HubConnection("http://localhost:54155/")
    2.  
    3.         Dim becare= connection.CreateHubProxy("BeCare")
    4.  
    5.         becare.On(Of IList(Of String))("newCases", Sub(data)
    6.                                                        If data IsNot Nothing Then
    7.                                                            Console.WriteLine(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") & " - Found " & data.Count & " new cases.")
    8.                                                            For Each s In data
    9.                                                                Console.WriteLine(s)
    10.                                                            Next
    11.                                                        Else
    12.                                                            Console.WriteLine("No records found")
    13.                                                        End If
    14.                                                    End Sub)
    15.  
    16.         connection.Start().Wait()
    17.  
    18.         Dim line As String = Nothing
    19.         Console.WriteLine("type exit + enter to exit.")
    20.         While line <> "exit"
    21.             line = Console.ReadLine()
    22.         End While

    And that's it.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    1 comment

    Comment from: ton snoei [Visitor]
    ton snoei Ever thought of 'long polling' or comet programming.
    11/29/12 @ 15:30

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)