Tuesday, August 11, 2015

how to work with SignalR

In real-time the server is aware of the data updates, event occurrences, etc.This is commonly achieved through methods like continuous polling, but this is incur a lot of traffic and load to the server.

So as an alternative SignalR is popup. SignalR is an Asp.Net library, which is designed to use the existing transport technologies underneath based on the client nature and the support it offers. 

This is capable of pushing the data to a wide variety of clients. So because of this new trend developers no need to worry about which server push transport but to use and deciding on the fallback in case of unsupported scenarios.

SignalR uses transports that are required to do real-time work between client and server. Each and every transports have its own requirement. So if these requirements are not met, SignalR will attempt to use other transports (one at a time) to make its connections.
  • WebSocket 
  • Server Sent Events
  • Forever Frame
  • Ajax long polling



Few Steps in a sample project:

Server setup


Define custom hub controllers inherits from the Base hub

In the Web Solution Hub folder with few custom hub classes all inherited from SignalR Hub.cs.

SignalR hub class provides methods that communicate with SignalR connections that connected to a Microsoft.AspNet.SignalR.Hub.


Hub class also provides the properties Clients, Groups, Context and events like OnConnected, etc. 


  • Format of the BaseHub.cs which is already defined.




  • Format of the PostHub.cs. All of the customer hubs are similar in format.
                   Groups manager for this hub instance
                   Context  provides information about the calling client.
                   DomainId added to make the instance more specific per domain.
                   OnConnected called when the connection connects to this hub instance.




Trigger SignalR js call in server side

Place the trigger in the relevant action method to call the client side

         GlobalHost provides access to default host information
         IConnectionManager provides access to hubs and persistent connections references
         GetHubContext return information specific to SignalR.Hubs
         Clients encapsulates all information about a SignalR connection
         Group initiates dynamic HubContextConnection with the parameters
         Function calls to the SignalR Javascript Ex: getTotalMessages()



Disconnect when needed

SingnalR keeps persistent connections once it start() the connection. But in the application session time out or logout need to call stop() to stop the connection.It defines in the Global.asax class.

              stopSignalR is dynamic opertaion to disconnect the SignalR connection




Client setup

State the hub connection in BpSignalR.js

              connection $.connection.hub.start()
              connection stop $.connection.hub.stop()



Define Logic in Js

   $.connection.postHub & BpSignalR.start() used to initiate dynamic connection
   postHub  camel case but same name as the custom hub class
   Navigation.postHub.client call server to Client else it should be server instead of client




Add reference in view

          SignalR reference needs to be place in relavent view to make it work 
         <script src="~/SignalR/hubs"></script>



Finally, This may help to undersatnd the flow of singnalR in current project I have working on.


Specially thanks to my tech lead Ranel Castro

References:

B' happiiiiiii always..............!

No comments:

Post a Comment