Showing posts with label SignalR. Show all posts
Showing posts with label SignalR. Show all posts

Saturday, November 2, 2013

SignalR 2.0 VB.NET Push Notification

Many readers have had difficulty with the post SignalR VB.NET Push Notification as they have installed the latest version 2.0. We will require some changes to the old post to make it work.

"..No more RouteTable.Routes.MapConnection but the new Owin MapSignalR..."
Let us review the steps...


Step 1


Install gituhub Package from Package Manager Console

PM> Install-Package Microsoft.AspNet.SignalR

Step 2


Add a new Class to project and create a PersistantConnection:

Imports System.Web
Imports System.Threading.Tasks
Imports Microsoft.AspNet.SignalR

Public Class NotificationConnection
  Inherits PersistentConnection
   Protected Overrides Function OnConnected(request As IRequest, connectionId As String) As System.Threading.Tasks.Task
      Return MyBase.OnConnected(request, connectionId)
   End Function
End Class

Step 3

Add our class for notifications (for simplicity we create note with a value and a time)

Imports Microsoft.AspNet.SignalR

Public Class Notification
   Private _ExamValue As Int32
   Private _Data As String
   Public Property ExamValue As Int32
      Get
         Return _ExamValue
      End Get
      Set(value As Int32)
         _ExamValue = value
      End Set
   End Property
   Public Property Data As String
      Get
         Return _Data
      End Get
      Set(value As String)
         _Data = value
      End Set
   End Property
End Class



Step 4 (Here is the update!!!)

No longer use the file Global.asax.vb. We add a new class for mapping connection. 
No more RouteTable.Routes.MapConnection but the new Owin MapSignalR

Imports Owin
Imports Microsoft.AspNet.SignalR
Imports Microsoft.AspNet.SignalR.Infrastructure
Imports System.Threading
Imports System.Threading.Tasks

<Assembly: Microsoft.Owin.OwinStartup(GetType(SignalR.Startup))> 

Public Class Startup
   Public Sub Configuration(app As IAppBuilder)
      app.MapSignalR(Of NotificationConnection)("/NotificationConnection")
      Task.Factory.StartNew(Function()
                               Dim connectonManager As IConnectionManager = GlobalHost.DependencyResolver.Resolve(Of IConnectionManager)()
                               Dim connection = connectonManager.GetConnectionContext(Of NotificationConnection)()



                               While True
                                  Dim item = New Notification() With { _
                                 .Data = DateTime.Now.ToString("hh:mm:ss"), _
                                 .ExamValue = CInt(Math.Ceiling(Rnd() * 100))
                              }

                                  connection.Connection.Broadcast(item)

                                  Thread.Sleep(1000)
                               End While
                            End Function)
   End Sub
End Class


Step 5 (also here a small change !!!)

In html page we have to change the reference to the old script with the new one for SignalR.

<script src="scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="scripts/json2.js" type="text/javascript"></script>
<!--<script src="scripts/jquery.signalR-1.1.1.js" type="text/javascript"></script>-->
<script src="Scripts/jquery.signalR-2.0.0.min.js"></script>
<script type="text/javascript">
    var output;
    $(document).ready(function () {

        output = $("#output");

    });

    var data;
    var options;
    connection = $.connection('/NotificationConnection');
    connection.received(function (dataItem) {
        var newItem = dataItem;
        output.text("Received exam value: " + newItem.Data + "," + newItem.ExamValue);
    });

    connection.start();
</script>
<body>
    <h2>Laboratory Exams Monitoring</h2>
    <div id="output"></div>
</body>

Thursday, May 23, 2013

SignalR VB.NET Push Notification


SignalR is a Microsoft library  still in pre release.

https://github.com/SignalR/SignalR

With SignalR we can implement the HTML5 WebSocket without worrying that the web server or the browser supports it. We can do this because the library itself will use long polling or classic polling as an alternative.

This technology finds its expression in chat rooms or online games, but we can think about health information systems, such as notifications to clients of alarms related to the results of laboratories (ok, I did not fancy, it's only my job :-) )

In this post we realize a system of push notifications in VB.Net updated to the latest release of SignalR, at least for now.


Step 1


First open visual studio 2012 and select new project: ASP.NET Empty web application.
Install gituhub Package from Package Manager Console

PM> Install-Package Microsoft.AspNet.SignalR


Step 2

Add a new Class to project and create a PersistantConnection:

Imports System.Threading.Tasks

Imports Microsoft.AspNet.SignalR
Public Class NotificationConnection
    Inherits PersistentConnection
    Protected Overrides Function OnConnected(request As IRequest, connectionId As String) As System.Threading.Tasks.Task
        Return MyBase.OnConnected(request, connectionId)
    End Function
End Class

Step 3

Add our class for notifications (for simplicity we create the note with a value and a time):
Imports Microsoft.AspNet.SignalR

Public Class Notification
    Private _ExamValue As Int32
    Private _Data As String
    Public Property ExamValue As Int32
        Get
            Return _ExamValue
        End Get
        Set(value As Int32)
            _ExamValue = value
        End Set
    End Property
    Public Property Data As String
        Get
            Return _Data
        End Get
        Set(value As String)
            _Data = value
        End Set
    End Property
End Class

Step 4


Critical step, the addition of the file Global.asax.vb, it specializes our router, which will do the job right at the time when the client creates a connection to the server

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
 RouteTable.Routes.MapConnection(Of NotificationConnection("NotificationConnection","/NotificationConnection")

Task.Factory.StartNew(Function()
Dim connectonManager As IConnectionManager =GlobalHost.DependencyResolver.Resolve(Of IConnectionManager()
Dim connection = connectonManager.GetConnectionContext(Of NotificationConnection)()
 While True
                Dim item = New Notification() With { _
                          .Data = DateTime.Now.ToString("hh:mm:ss"), _
                          .ExamValue= CInt(Math.Ceiling(Rnd() * 100)) 
                          '/* For simplicity we simulate the result of examination with a randomic value 
                        }

               connection.Connection.Broadcast(item)

               Thread.Sleep(1000)
  End While

   End Function)
End Sub

Step 5


Now we move on the client and build our html page:

<script src="scripts/jquery-1.6.4.js" type="text/javascript"></script>
<script src="scripts/json2.js" type="text/javascript"></script>
<script src="scripts/jquery.signalR-1.1.1.js" type="text/javascript"></script>
<script type="text/javascript">
    var output;
    $(document).ready(function () {

        output = $("#output");

    });


    var data;
    var options;
  connection = $.connection('/NotificationConnection');
    connection.received(function (dataItem) {
        var newItem = dataItem;
        output.text("Received exam value: " + newItem.Data + "," + newItem.ExamValue);
    });

    connection.start();
</script>
<body>
<h2>Laboratory Exams Monitoring</h2>
<div id="output"></div>
</body>

That's it, from now our good server will update us every second with the value of the exam to be observed, we can think to plot the trend or change the Application_Start() to retrieve values ​​from the laboratory and just update us in case of alert.