SignalR: Call client methods from Custom ActionFilter class and Publish messages (Render MVC View as a string) to all subscribed clients

Sometimes we would like to perform certain action before or after a particular operation, or some times we need a pre or post action behaviors from action, for achieving this functionality ASP.NET MVC provides a feature called Filters. Action filters are used to implement the logic that get executed before or after a controller action executes.

Here I use Result Filter to call client method and  send a ViewResult  as a string parameter to client method.

Please Refer Demo project using SignalR 2 and MVC 5 to create a SignalR project in MVC.

Step 1: Create ‘ SignalRSendMessageAttribute ‘ Class derived from  ActionFilterAttribute  and  add following content inside derived class.

SignalRSendMessageAttribute.cs


 public class SignalRSendMessageAttribute : ActionFilterAttribute
{

 public SignalRSendMessageAttribute()
{

}

public override void OnResultExecuted(ResultExecutedContext filterContext)
{
   base.OnResultExecuted(filterContext);
   ViewResult result = filterContext.Result as ViewResult;

  if (result != null )
   {
  string fullView = GetStringFromView(result, filterContext);

// call  client Method from  ActionFilter Attribute

// ChatHub is  a Hub used  in SignalR Demo Project. Blog link already mentioned in top section. please go through it.

 var context = GlobalHost.ConnectionManager.GetHubContext<ChatHub>()
//Broadcast to all clients
 context.Clients.All.sendMessage(fullView);
//or
// Broadcast only to clients in a group.
context.Clients.Group("groupName").sendMessage(fullView);
 }
}
// Get a string from MVC View
private string GetStringFromView(ViewResult result, ResultExecutedContext filterContext)
{
using (var sw = new StringWriter())
 {
 var viewContext = new ViewContext(filterContext.Controller.ControllerContext, result.View, result.ViewData, result.TempData, sw);
result.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
 }
}
}

Apply ActionFilter to Controller

Action Filters can be applied to either controller action or controller itself, with the help of action filter we can change the way, the action or controller gets executed.  For example, by applying SignalRSendMessage  attribute to Controller, it will send a message to all subscribed clients in each action methods of that controller.


[SignalRSendMessageAttribute]

public class ChatController : Controller
{

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

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

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

}

Client Method

readerWriter.client.sendMessage = function (domObject) {

// add this HTML Content  inside any MainDiv.

document.getElementById(“MainDiv”).innerHTML = domObject;

// execute  all script exist in domObject HTML Content.

var arr = MainDiv.getElementsByTagName(‘script’)
for (var n = 0; n < arr.length; n++)
{
eval(arr[n].innerHTML);
}

};

 

Thanks 🙂

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.