Real Time Development With SignalR

Generally, if you are developing an application which needs real-time information (give the information as soon as it is available), the first idea which comes to your mind is - create a timer, which will call a function, based on some time interval again and again. The function retrieves the database record and you check whether new information is available or not.

This approach is not real time and it uses a lot of resources in both sides (Server, Client) and if your device does not have enough resources, the Application will hang or the UI will not respond. 
What is the solution?
The solution is to use something which is made for real-time communication, and we have a library from Microsoft known as Signalr. 
Signalr is a library for .NET developers for adding real time functionality to the Application made using .NET technology (ASP.NET Application, C# console Application, WPF Application, Windows phone Application etc.). It is open source and you can download it from GitHub.
The GitHub URL for Signalr. 
What  you can create 
You can create all different types of applications which need real time functionality.
  • Chat Application.
  • Application which requires Real time notification.
  • An online game with high frequency updates.
How It Works 
The Signalr uses the concept of socket. Now, you may have the question, what is socket?
Let's understand it by example- consider an electrical socket, which is waiting for the plugs to be connected and will start transferring its current as soon as a suitable plug will be connected.

Now, replace the electric socket with 'Server', plug with 'Client', and current with 'data'.
Now, again read the whole sentence - a server is waiting for the clients to be connected and will start transferring its data as soon as a suitable client will be connected.
Now, it sounds like real time communication.
Thus, the socket consists of two things -
  1. Server
  2. Client (may be multiple)
In the above scenario,there is a single hole in the electric sockets but there may be multiple holes, say unlimited, for instance, so multiple plugs can be connected.
The Server can have multiple clients for a single Server and each client will receive the data based on some mechanism defined under the Server. 
Now, let's come to the Signalr, 
The Signalr provides two types of API  we need for the socket communication.
  1. API for creating Server.
  2. API for creating clients of the Signalr Server. 
Currently, the client API is available for .NET language, JavaScript, Java, Node.js but you will have to create the Server in .NET environment.
Real time example
It feels only thoughtful and imaginary until and unless, we compare it with some real time example, which is already implemented and working successfully.
The popular examples of real time communication are Whatsapp, Hike, Wechat etc. so, the next question will be how we will create an Application the same as Whatsapp, using Signalr.
We can create an Application the same as Whatsapp, using .NET language. Now you may think I am joking but I am not. Believe me, you can create an app same as Whatsapp or maybe better. It all depends on your coding ability, creativity, hardwork and most important if you are able to understand this article completely.
How to create a Real time app
If you want to create an app similar to an existing app - think about it, how it is working?
Thus, let's figure out how Whatsapp works -
  • When you install Whatsapp - you sign it with your mobile number, so mobile number is stored as ID, which is used to identify you.
  • Afterwards, we send a message to someone, who is on Whatsapp by using their mobile no, because Whatsapp identifies people by their mobile no.
  • When we send a message to someone, the message goes to the chat Server, the Server figures out whom to send the message to and sends the message.
  • When the message is sent successfully to the Server, Whatsapp marks the message with a single right mark.
  • When the message is sent to the receiver, the Server sends an acknowledgement to the sender that message has been sent and Whatsapp marks with double right marks.  
This is how Whatsapp works. Thus, you may have got that - Whatsapp is a client, who needs a Server to send the message. Thus, the first thing that we need is a chat Server. Thus, we will use Signalr to create the server. Now, consider, our Server is created.
What else do we need?

We need a client to communicate. Let's create a Windows phone app as a client, using Signalr .NET API. Now consider, our client is created and our chatting app is working well but nowadays, we need to create the app for multiple platforms- like Android, Web etc. Thus, how to create it?
As I have said - Signalr provides API for Java, JS, Node.js. Thus, you can create the client, using the Signalr API for different platform.
Hurrray! Now, you must have got the idea, how a real time Application works and how you can create real time apps, but again, it seems only imaginary until and unless, we create it. 
 Want to create some
Check out my articles on signalr.