0

I have created a sample signalR for POC. I want to call a hub method from Global.asax and pass a string value to client. My Message hub is :-

[HubName("messageHub")]
public class MessageHub : Hub
{
    public static IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
    public void Message()
    {

        /*
         * services and updates the messages
        * property on the PushMessage hub
        */
        //IHubContext context = GlobalHost.ConnectionManager.GetHubContext<SignalR_Error_Logging.Models.ErrorModel>();
        List<GenerateError.Repository.ErrorModel> model = ErrorRepository.GetError();
        context.Clients.pushMessages(model[0].ErrorMessage);

    }

I have defined two of the scripts in layout.cshtml

<script type="text/javascript" src="../../Scripts/jquery-1.6.4.js"></script>
<script type="text/javascript" src="../../Scripts/jquery.signalR-0.5.3.js"></script>

My Index.html is as below:-

    @{
    ViewBag.Title = "Receive Error message";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<script src="/signalr/hubs" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var myHub = $.connection.messageHub;
        myHub.pushMessages = function (value) {
            console.log('Server called addMessage(' + value + ')');
            $("#messages").append("<li>" + value + "</li>");
        };
        $("#btnMessage").click(function () {

            myHub.message();
        });
        $.connection.hub.start().done(function () { alert("Now connected!"); }).fail(function () { alert("Could not Connect!"); });
    });

</script>
<h2>Receive Error Messages</h2>
<ul id="messages"></ul>
<input type="button" id="btnMessage" value="Get Error" />

In Global.asax I have written

SignalR_Error_Logging.SignalRHub.MessageHub hub = new SignalRHub.MessageHub();
        hub.Message();

In Application_Start();

I am not able to display message in my UI(i.e Index.cshtml).

Things that i have tried:-

  • Running the application as IIS.
  • Changing the way of creating HubContext.

        IHubContext _context = GlobalHost.ConnectionManager.GetHubContext<MessageHub>();
    context.Clients.notify("Hello world");
    
  • if (Clients != null) { Clients.shootErrorMessage(message); this.Clients.shootErrorMessage(message); }

  • Gone thru links of StackoverflowCalling SignalR hub clients from elsewhere in system

Any advice???

When i call my hub method by creating a button in Index.html it works fine.

Apologies for not framing my question properly!!

Community
  • 1
  • 1
Shubh
  • 6,279
  • 8
  • 42
  • 74

2 Answers2

2

I figured out, way of calling the Hub was not correct. For the time being i have modified my code as in Global.asax :-

 private void CallSignalR()
    {
        var context = SignalR.GlobalHost.ConnectionManager.GetHubContext<SignalR_Error_Logging.SignalRHub.MessageHub>();
        List<GenerateError.Repository.ErrorModel> err = GenerateError.Repository.ErrorRepository.GetError();
        foreach (var item in err)
        {
            item.ErrorDescription = item.ErrorDescription + DateTime.Now.ToString();
        }
        context.Clients.pushMessages(err);


    }

Works absolutely fine now :)

Still figuring out better alternatives !!!!

Shubh
  • 6,279
  • 8
  • 42
  • 74
1

This will never work. Application_Start() in Global.asax is only called once for the lifetime of your AppDomain. It happens when the website starts up and at this point in time, no clients are connected yet (as the website isn't fully initialized) so you can't send messages via SignalR.

Alexander Köplinger
  • 2,477
  • 14
  • 15
  • Thanks.. :) You are absolutely correct. But when i have one instance running and i restart my VS2012. It should work. Anyway's.. I figured out that the way of calling my hub method was wrong. Thanks again!!! – Shubh Oct 12 '12 at 13:29
  • Seems like if the answer is correct, it should be marked as such. – Byron Sommardahl Nov 02 '12 at 22:03