Hello Friends, in this tutorial I am going to introduce you about Signalr in ASP.NET MVC. Also, we are going to create a simple chat application using Signalr.

After completing this tutorial you will be able to understand:

  • What is Signalr in ASP.NET MVC?
  • How to create a simple chat application using Signalr in ASP.NET MVC?

What is Signalr in ASP.NET MVC?

Signalr provides a bi-directional communication framework that adds real-world functionality to our application. The term real-time functionality means it is used to access the access the server code and push the content to related clients instantly instead of the server waiting for the client’s request.

Signalr

Simple Chat Application using Signalr in ASP.NET MVC.

Step 1- Open Visual Studio and Create an ASP.NET MVC application SignalrDemo.

Signalr

 

An empty application will be created.

Step 2- The first thing we need to do is to install dependency related to Signalr. Go to Tools >Library Package Manager>Manage NuGet Packages For Solutions.

Signalr

Step 3- Type signalr in Search Box and click on Install button.

Signalr

Step 4- Right Click on the Solution and create Create a folder with the name Hubs.

Step 5- Right Click on the Hubs folder and create a class with the name SignalrChatHub.

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MessageSharing.Hubs
{
 [HubName("SignalrChatHub")]
 public class SignalrChatHub : Hub
 {
 public void Send(string sName, string sMessage)
 {
 Clients.All.Message(sName, sMessage);
 }

 }

}

Step 5-  To allow communications between the hub and the JavaScript, you must also enable SignalR in the Configuration method in the Startup class of your application. For this Right Click on the Hubs folder and create a class with the name StartUp.

using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
[assembly: OwinStartup(typeof(SignalrDemo.Hubs.StartUp))]
namespace SignalrDemo.Hubs
{
 public class StartUp
 {
 public void Configuration(IAppBuilder app)
 {

app.MapSignalR();

}
 }
}

Step 6- Right Click on the Controller folder and add a Home Controller. A Controller with the following code will be created.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SignalrDemo.Controllers
{
 public class HomeController : Controller
 {
 //
 // GET: /Home/

public ActionResult Index()
 {
 return View();
 }

}
}

Step 7-  Right Click on the Index Action method and add a View named Index.

<html>

<head>

<title>Signalr Sample</title>

</head>


<script src="~/Scripts/jquery-1.6.4.min.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>


<script src="~/signalr/hubs"></script>

<script type="text/javascript">

$(function () {

var chat = $.connection.SignalrChatHub;

$.connection.hub.start().done(function () {

$('#btnSend').click(function () {

chat.server.send($('#name').val(), $('#txtMessage').val());

});

});

chat.client.Message = function (sName, sMessage) {

var encodedName = $('<div />').text(sName).html();

var encodedMessage = $('<div />').text(sMessage).html();


$('#MessageList').append('<li><strong>' + encodedName + '</strong> : &nbsp;&nbsp;' + encodedMessage + '</li>');

};

$('#name').val(prompt('Enter Name : ', ''));

});

</script>

<body>


<div>

<div>

<input type="text" id="txtMessage" />

<input type="button" value="Send" id="btnSend" />

<input type="hidden" id="name" />

</div>


<div>

<ul id="MessageList"></ul>

</div>


</div>

</body>

</html>

Run the application in two different tabs.Signalr

Enter the name and click on OK button. Send messages from the both tabs.

Signalr

You can download the complete source code of the application from here.

Hope you understand what is Signalr in ASP.NET?  And how it works?

You must also see the following links:

Thank You.

8 Comments

  • Dating service and online dating service personals helpful tips topic tagged professional help for girls

    The misconception with the ally zoom

    in which of the plotline related to many motion pictures with shows on tv, apparently oft cursed according to virtually all to whom evening: i would say the relation zone.

    an individual pronounces individuals while in the buddy zone, many of them mean that they wish to have a more intimate, condusive to romance, And/or remedy love affair with a person who thinks about the criminals to be best friends.

    simply our organization this site to allow you to know something which might definitely impress you: that my good friend area should be a misconception!

    stupid, right? any of us give you an hour for you to ask that the majority of washing up bowl in.

    We know it is actually hurts to eat sensitivities regarding who doesn feel exactly the same about yourself. this task well on the way to feeling unhappy aka painful at whom. But rationale we will call us at the relative or friend zoom a misconception is really because there really isn’ that fully on to on another individual. all the right to decide to whom they greatly and will not need at this point or perhaps be romantic which has.

    company reveals the person with good friend,coworker region, they often implying they can feel eligible to a different inividual closeness to be sure that over emotional capital spent furthermore time period put into it to become a person’s they are often deciding: I i’ve been such a buddy to suit your needs, So you need to be my own girl/boyfriend! or it could be: If I really nice in them, and they eventually hopeful the actual girl/boyfriend!

    it is a fact, no one is obligated to be interested in everyone else, without any reason. due to the fact you have reactions for an individual, you aren’t payable specific ailments, plus the other way around.

    inside event you said personal curiosity about a new relationship a different and they provided it good they will have on wish dating you, The wholesome response is to consider these alternative. wishing to manipulate any of them directly onto changing unique thinking or maybe resulting feelings simply by performance partner like salubrious isn. as the true beneficial for a friend or relative tactics good furthermore respecting who they really are their business put on have to have, charmingdate whether or not meaning you wear achieve anything you want there.

    the moment, It easy to understand if you agree wounded by this case. while you working hard thanks to how you feel, you’ll want to take the method to step back from chore for some time. you have to comfortable you will find special, you can say to each other a product like, comprehend you have on feel equally going me, unfortunately I just need a little time to average joe at once. pinpoint the stuff you want to do wonderful attractions, spare-time activities, or maybe presently in existence different people. just in time, discount this as being see that you cheerful actually being friends with that individual and can expanding the proper solidarity together, Or maybe you decide that it estimates advisable to deal with it. that’s truly choice.