Saturday, July 27, 2013

How SignalR Works

SignalR is a library which can add realtime web functionality to applications. It provides a simple API for creating server to client RPC, that call JavaScript functions from server side .NET code.SignalR handles connection management automatically. It also has a API for this.

SignalR is an abstraction over a connection. It gives you 2 programming models over the connection (Hub and Persistent Connection). 

SignalR applications can scale out to thousands of clients using Service BUS, SQL Server or Redis. 

Supported transports by SignalR

SignalR connection starts as HTTP, and is then promoted to WebSocket connection if it is available. If WebSocket is not available it falls back to older transports.

HTML5 Transports
  • WebSocket
This is the ideal technology for SignalR because it uses server memory efficiently. It has the lowest latency and full duplex communication between client and server. But WebSocket requires Windows Server 2012 or Windows 8 and .NET framework 4.5. 

Basically to use WebSocket both client and server should support WebSocket.
  • Server Sent Events
Also known as EventSource. Does not support in IE. :-(

Comet Transports
Here browser or other client maintains a long-held HTTP request, which server can use to push data to client without client specifically requesting it. 
  • Forever Frame
For IE only. Creates a hidden IFrame which makes a request to an endpoint on server which does not complete. 

The server then continually sends script to the client which immediately executed, which provides one way real time connection from server to client. The connections server to client and client to server uses separate connections. A new connection is created for each piece of data that needs to be sent.
  • Ajax Long Polling
Does not create a persistent connection, instead polls the server with a request that stays open until the server responds, at which time the connection closes. and a new connection is established(requested) automatically. This may introduce some latency when connection gets resets


You can enable logging for hub's events in a browser using $.connection.hub.logging = true; command. 

If the client capabilities are known, transport can be specified when client connection is started using connection.start({ transport: 'longPolling' }); command. you can also specify fallback order like connection.start({ transport: ['webSockets','longPolling'] });

Connections and Hubs

SignalR has PersistentConnection and Hub connections. Persistent connection API provides direct access to low level communication protocols. A Hub is a more high level pipeline built on top of Connection API. 

Using Hubs also allows you to pass strongly typed parameters to methods, enabling model binding.

Self Hosting SignalR applications
Hosting SignalR is not restricted to IIS. Using SignalR Self Host library which is built on OWIN you can host SignalR on Console applications and Windows services. Check this article.

In the ASP.NET SignalR space there are lot of articles you can look into.


Here we limit client and server to a limited no. of times to be communicated (e.g: 25 times per second)




Resources:

0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook