SignalR is a new library for .NET software development companies that is used to add real time web functionality to ASP.NET applications. Real Time functionality is the ability to have put your server side code content to connected clients instantly as it becomes available, rather than having the server wait for a client to request a new data.
SignalR provides two communication levels:
- Persistent Connection API - low level, message-oriented communication
- Hub API - high level, allows direct call of client and server methods
The Hub API offers the most convenient and preferred communication API. The Persistent Connection API might only be interesting, if you need to integrate with an existing message based server application or if you need for some reason detailed control on the message level.
How it is different?
SignalR is the latest library in ASP.Net, the 'real-time' framework that allows the server to execute methods on the client. We are not limited to this interaction, and we can expose server-side methods to our clients. This potentially allows for a much cleaner interaction between client and server, as all data is passed through arguments or the result of a method call. SignalR clients are available for a variety of platforms, and a client-side is generated for all of the server-side methods that are exposed to the clients.
Analysis of ASP.NET SignalR
- Not restricted to web clients
- Selection of the best available communication mechanism automatically
- Binding complex objects (JSON)
- Open-source solution available on GitHub that can be installed via NuGet
- Bidirectional communication for (web) clients
- Easy to setup
- Install by issuing NuGet command: “install-package Microsoft.AspNet.SignalR“.
- SignalR can run inside a web application or self-hosted.
- Easy to debug. It can run in IIS Express. All the powers of Visual Studio just wait to be utilized.
- Very good documentation and tutorials
- Not a reliable communication
- ASP.Net Software development companies should not use it if a guaranteed delivery of messages is mandate. Bank Transaction are a negative example.
- It uses dynamics. Some syntactical errors can only be detected at runtime. However, only a few lines of code are affected. So, it’s not a real problem detected all errors related to the usage of dynamics.
- At times, it’s difficult to attach a debugger to the running process, especially, under heavy load.
- Allows only 10 connections when running in IIS in desktop systems.
- Web clients and the server use different technologies so there is not much code reuse between them.
Simple Chat Application with MVC
- In Visual Studio, create new project with C# ASP.NET Web Application and click ok.
- In the New ASP.NET Project window, select MVC, and click Change Authentication.
- SelectNo Authentication in the Change Authentication window, and click OK.
- Click OK in the New ASP.NET Project
- Open theTools | Library Package Manager | Package Manager Console and run the following command. This step adds to the project a set of script files and assembly references that enable SignalR functionality.
- In Solution Explorer, expand the Scripts folder. Note that script libraries for SignalR have been added to the project.
- In Solution Explorer, right-click the project, select Add | New Folder, and add a new folder named Hubs.
- Right-click theHubs folder, click Add | New Item, select the Visual C# | Web | SignalR node in the Installed pane, select SignalR Hub Class (v2) from the center pane, and create a new hub named cs. You will use this class as a SignalR server hub that sends messages to all clients.
- Replace the code in theMessageHub class with the following code.
- Create a new OwinStartup class called Startup.cs and press Add.
- Change the contents of the Startup.cs file to the following.
- Edit theHomeController class found in Controllers/HomeController.cs and add the following method to the class. This method returns the Chat view that you will create in a later step.
- Right-click the Views/Home folder, and select Add | View.
- Replace the contents ofcshtml with the following code.
- Save All for the project.
Run the SignalRDemo
- Press F5 to run the project in debug mode.
- In the browser address line, append/home/chat to the URL of the default page for the project. The Chat page loads in a browser instance and prompts for a user name.
- Enter a name.
- Copy the URL from the address bar of the browser and use it to open two more browser instances. In each browser instance, enter a unique user name.
- In each browser instance, add a comment and click Send. The comments should display in all browser instances
- The following screen shot shows the chat application running in a browser.
- Type message and press sent and check with two different browser instance
It also enables completely new types of applications that require high frequency updates from the server, e.g. chat application, real-time gaming, Application Monitoring, etc.