locked
SignalR - Trying to send a message from controller RRS feed

  • Question

  • User-50596518 posted

    Hi!

    Hope somebody could help me... I'm trying to push a notification every time I create an element from the controller... so, basically, what I found in the internet so far is shown below. What am I doing wrong?

    HUB

    namespace MesaServicio.Hubs
    {
        public class NotificationsHub : Hub
        {
            public void SendNotification(string nombre, string resumen) 
            {
                Clients.All.sendNote(nombre, resumen);
            }
        }
    }

    CONTROLLER

            void SendNotification(int id)
            {
                Ss ss = db.Sses.Find(id);
    
                var myHub = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
    
                myHub.sendNote(ss.SsNombre, ss.Resumen);
            }

    VIEW - JAVASCRIPT

    @section Scripts{
        @Scripts.Render("~/bundles/jqueryval")
    
        @Scripts.Render("~/Scripts/jquery.signalR-2.2.0.min.js")
        @Scripts.Render("/signalr/hubs")
    
    <script>
    
    $(function () {
    	startNotificationHub();
    });
    
    function startNotificationHub() {
                var connection = $.connection.hub,
                    hub = $.connection.notificationsHub;
    
    
                hub.sendNote = function (nombre, resumen) {
                    var msg = "<li><a href='javascript::;'> " +
                        "<i class='menu-icon fa fa-file-code-o bg-green'></i>" +
                        "<div class='menu-info'>" +
                        "<h4 class='control-sidebar-subheading'>" + nombre + "</h4>" +
                        "<p>" + resumen + "</p>" +
                        "</div>" +
                        "</a></li>";
    
                    $("#notifications-vca").append(msg);
                };
    
    
                $.connection.hub.start();
    
    }
    
    </script>
    
    }

    Tuesday, June 7, 2016 12:14 AM

All replies

  • User61956409 posted

    Hi proinnovaperu,

    I'm trying to push a notification every time I create an element from the controller

    You could refer to the following sample to modify your code to broadcast message to all connecting clients.

    var myHub = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
    
    myHub.Clients.All.sendNote(ss.SsNombre, ss.Resumen);
    

    Best Regards,

    Fei Han

    Tuesday, June 7, 2016 2:16 AM
  • User-50596518 posted
    Thank you Fei... I'll try first time tomorrow and let you knlw the outcome.
    Tuesday, June 7, 2016 3:03 AM
  • User-50596518 posted

    Hi Fei!

    The fix suggested works fine if it's triggered from a button like this:

     $("#prueba2").click(function () {
                    hub.sendNote("@Model.ServiceSession.SsNombre", "@Model.ServiceSession.Corp.CorpName", "@Model.ServiceSession.Tipo");
                });

    But this ActionResult is not

      public ActionResult PruebaHub(int id)
            {
                SendNotification(id);
    
                return RedirectToAction("Details", new { id = id });
    
            }
    
            public void SendNotification(int id)
            {
                Ss ss = db.Sses.Find(id);
    
                var myHub = GlobalHost.ConnectionManager.GetHubContext<NotificationsHub>();
    
                myHub.Clients.All.sendNote(ss.SsNombre, ss.Corp.CorpName, ss.Tipo);
    
            }

    is it probably because the "RedirectToAction" undo any render?
     

    Tuesday, June 7, 2016 4:18 PM
  • User61956409 posted

    Hi proinnovaperu,

    is it probably because the "RedirectToAction" undo any render?

    Firstly, please debug the code to make sure if there are errors with SendNotification(int id) snippets.

    Secondly, you could try to comment out RedirectToAction then check if same issue will appear.

    Best Regards,

    Fei Han

    Thursday, June 9, 2016 9:57 AM
  • User-50596518 posted

    Hi Fei!

    No errors on debugging... cool!

    I had isolated the function outside the ActionResult (to avoid a RedirectToAction) and it worked...it actually appends to my <ul> ... awesome!

    Here is the challenge now: it won't replicate in other windows. And there's also the fast that it wont persist when changed to another view (considering my <ul> is inside a Partial View which it's inside _Layout)

    And additional observation: because this "notifications" have to be fired after a new register is created (by a Controller "ActionResult" of "Create") which as a return action is a Redirection to the "Detail" view of the new element, what I did was to pass a parameter from "Create" view to "Detail" view:

     return RedirectToAction("Details", new { id = ss.IdSs, linkTVcode = ViewBag.GetTVcode, newCreated = "true" });

    Then (I know it's too much...), I assigned that parameter to a hidden <input>

    <input type="text" id="inputNewItem" value="@ViewBag.NewCreated" hidden />

    Which will fire the hub  using jQuery (has to be an easier way to do it... I'm just such a newbie):

     if ($('#inputNewItem').val() == "true") {
                    hub.sendNote("@Model.ServiceSession.SsNombre", "@Model.ServiceSession.Corp.CorpName", "@Model.ServiceSession.Tipo", "@Model.ServiceSession.IdSs", "Creado por: @Model.ServiceSession.UsuarioCreador");
                }

    Regards!

    Thursday, June 9, 2016 9:19 PM