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:

Wednesday, July 17, 2013

Getting the most out of Global.asax

Global.asax is a class derived from HttpApplication class. This file is also called ASP.NET application file. Global.asax is responsible for handling application level events raised by ASP.NET and HTTPModules. This file is optional if you haven't created it, ASP.NET assumes you have not defined any application or session event handlers.

During the lifetime of your application, ASP.NET maintains pool of Global.asax derived HttpApplication instances. When the application receives an HTTP request, ASP.NET page framework assigns one of these HttpApplication instances to process the request. That instance is responsible for managing the request throughout its lifetime. When you see many number of requests coming to the application, many instances of HttpApplication instances is expected

Inside Global.asax file there are many methods you can use to make your lives easier when developing ASP.NET applications. This article will look at some of the methods you can use in your web applications.

ASP.NET automatically binds application events to handlers in Global.asax using 'Application_event' naming convention. (How to ASP.NET Application_Events Work - Rick Strahl and SO Question).

Before digging into Global.asax file you must understand the sequence of how each method is called. Below diagram extracted from stackoverflow shows the sequence.


Global.asax event sequence

Application_Start and Init (source)
Application_Start is a special method which doesn't represent HttpApplication events (Also Application_End). ASP.NET calls them once for the lifetime of the application, not for each HttpApplication instance. On the other hand, Init is called once for every instance of HttpApplication after all modules have been created.

Application_End
Application_End will fire when IIS Pool is recycled or the application is unloaded. (If a dependent file such as web.config gets changed, application will reload).

PreSendRequestHeaders (link)
Occurs just before ASP.NET sends HTTP headers to the client. 

BeginRequest
First event of the HTTP pipeline chain of execution when ASP.NET responds to a request

EndRequest
Occurs as the last event 

Error handling
Resources You can handle application errors inside Application_Error method


Accessing Session Data inside Global.asax
http://stackoverflow.com/questions/765054/whens-the-earliest-i-can-access-some-session-data-in-global-asax
http://stackoverflow.com/questions/5977285/set-session-variable-in-application-beginrequest?lq=1

Access Global.asax properties some outside (SO)
 
Resources
MSDN, TechRepublic, Application life cycle for IIS 5.0 and IIS 6.0, Application life cycle for IIS 7.0
Check Create Custom HTTP Modules

Saturday, July 6, 2013

Database Indexes

Database index is a data-structure which improves retrieval of data from database tables. Indexes are used to quickly locate data without having to go through every row in database table. 

Source : kindleyourbrain
If you create an index it'll create a data-structure with the field value in which you created the index and a pointer to the record in the original table.The values in an index is sorted.

The downside of creating indexes is it requires additional disk space. Also when you have many indexes data writing will be bit slower because you need add a record to index data structures as well.

You can use indexes to tune performance of the database. See how to work with SQL Server Indexes here

Check below video to learn more about indexes. Also you check database indexes videos in Youtube.

Clustered vs NonClustered indexes

A clustered index is a special kind of index means you're telling the database to store similar values close to one another on the disk. This is the reason why you can have only one clustered index for a table. This has the benefit of rapid retrieval of records. By default a column with a primary key already has a clustered index.

Index must knows
https://www.simple-talk.com/sql/performance/14-sql-server-indexing-questions-you-were-too-shy-to-ask/

You can have many nonclustered indexes.



Resources

Thursday, July 4, 2013

Storage options in HTML5

 This document is in draft version   

In this article we'll look into storage options available for HTML5. 

Web storage

  • Store data locally within user's browser. Earlier this was done using cookies. 
  • But Web Storage is secure and much faster. 
  • The data is not included with each server request. 
  • Possible to store large amounts of data without affecting site performance
  • Stored in key-value pairs
localStorage object stores the data with no expiration date. 

// Store
localStorage.setItem("lastname", "Smith");
// Retrieve
document.getElementById("result").innerHTML = localStorage.getItem("lastname"); 


sessionStorage object is like localStorage object but it stores data only for one session. The data will get deleted when user closes the browser window

if (sessionStorage.clickcount) {
    sessionStorage.clickcount = Number(sessionStorage.clickcount) + 1;
} else {
    sessionStorage.clickcount = 1;
}
document.getElementById("result").innerHTML = "You have clicked the button " +
sessionStorage.clickcount + " time(s) in this session.";
  

AppCache API

Cache the web application so that it is accessible without internet
  • Offline browsing - Use the application offline
  • Speed - Cached resources load faster
  • Reduced server load - Will only load updated/changed resources from server
AppCache API is supported from IE 10 onwards and other browsers. 

To enable app cache, you must include manifest attribute in documents <html> tag

 <!DOCTYPE HTML>
<html manifest="demo.appcache">
...
</html>


Every page with manifest attribute specified will be cached when the user visits it. The recommended file extension for manifest files is '.appcache'.

Geolocation API
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