Introduction
SignalR is a ASP.NET server library for adding real-time functionality to a website. SignalR Simply is a process of adding real-time functionality to web applications. Real time describes a human rather than a machine sense of time or in other words something that happens with us at a specific time. It provides the bidirectional communication for the client and server both. SignalR provides us the facility of easily creating real-time websites and web applications.
Using SignalR in projects
The following summarizes why developers should choose SignalR for project:
- If we are creating a web application then it is important that it will be directly connected to the server for the communication purposes.
- It is open source.
- We can provide notification in the format of alerts and comments.
- We can provide chat in a group or something one-to-one.
How SignalR Works
In this image the client sends a request to the server and the server responds after checking whether it supports the data transportation technique or not using SignalR. Some data transportation technique is used as in the following:
- Web sockets
- Forever Frames
- Server Sent Events
- Long Polling
If the client supports any of these techniques then the server creates a connection between them and starts the conversations.
How to implement SignalR in our project
The following are the three main steps for working with SignalR:
- Installing Visual Studio10
- NuGet package manager
- Download SignalR
You will need NuGet setup within Visual Studio 10 to download SignalR. If you are using Visual Studio 2012 then NuGet comes preinstalled by default. First we have a need of Visual Studio 2012 because in this version NuGet is provided with this version for using SignalR but don't worry if you have Visual Studio 2010, just install NuGet and then use it. Now to get started, we have a need to first create an empty ASP.Net
website and here we go.
Step 1
Open Visual Studio 2012 like this and choose New Project.
Now choose ASP.Net Empty Web Application as shown below.
Now the web site is created. Go to the Solution Explorer and right-click on the project and choose "Manage Nuget Package".
After opening it search for the SignalR package. Now install the ASP.Net SignalR package and when the package is installed then it is good congress this time you are ready to go. After installing it make your application easily.
Add your classes and HTML pages as you like for implementation.
Your page looks as in that after creating a class.
And after choosing a HTML page your page should look like this:
Now you are ready to create your website with the use of SignalR. You have two classes for use and one HTML page use your on code like me. This is my first code in Visual Studio 2012 with SignalR. It shows a real-time example of after clicking on "click here" your page will be opened in a new window. Use this code and make a real-time website. My source code is below and then their outputs of open new window after clicking click here.
- <! DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <Head>
- <Title>Demo</title>
- </head>
- <Body><style=backgroundcolor:"pink">
- <div id="example">
- <div id="grid" style="margin-bottom:10px"></div>
- <span id="alert"></span>
- <Script src="./scripts/jquery.signalR-2.2.0.min"></script>
- <Script>
- $(function ()
{
- var myurl = "";
- var connection = $.hubConnection(myurl, { useDefaultPath:
- false });
- var myhub = connection.createHubProxy("productHub");
- var myhubStart = connection.start({ jsonp: true });
- $("#alert").myAlert ({
- width: "100%",
- position: {
- top: 1,
- left: 1
- }
- });
- $("#grid").myGrid(
{
- height: 650,
- editable: false,
- sortable: false,
- columns: [
- { field: "DateOfBirth" },
- { field: "Password" },
- { command: "destroy", width: 200 }
- ],
- toolbar: ["create"],
- dataSource: {
- type: "signalr",
- autoSync: true,
-
- occur
- push: function (a) {
- var alert = $("#alert").data("myAlert");
- alert.success(e.type);
- },
- access:
{
- versaion: {
- id: "ID",
- fields: {
- "ID": { editable: false, nullable:
- true },
- "DateOfBirth": { type: "date" },
- "Password": { type: "number" }
- }
- }
- },
- sort: [{ field: "DateOfBirth", dir: "desc" }],
- implement:
- {
- signalr: {
- fact: myhubStart,
- hub: myhub,
- server: {
- fatch: "read",
- update: "update",
- destroy: "destroy",
- create: "create"
- },
- client:
- {
- fatch: "read",
- update: "update",
- destroy: "destroy",
- create: "create"
- }
- }
- }
- }
- });
- });
- </script>
- <div class="box" style="margin-left: 120px">
-
- <h1><b> My Frist demo page for best define a real time
- example using signalR</b></h1>
- <p> My example is the
- demo of real-time push notification <a href="http:
- sharpcorner.com/tags/1/signalR-example">More About SignalR</a>.If you
- are interesting to see the real time updates then:</p>
- <ul>
- <li> for Open this page in
- another browser window by clicking <a href="./signalr"
- target="_new">here</a>
- </li>
- <li> Create,
- update or destroy grid items.</li>
- </ul>
- <p><strong> Remark:</strong> I hope
- this demo provides you a better understanding. Please wait for my
- next artical.</p>
- </div>
- </div>
- </body>
- </html>
The output when you click on click here the next window will open.
Summary
In this article we have learned about SignalR, how it works and how to implement it in our websites for adding real-time functionality.