1

I'm using MVC 5 and I have my hub class like this :

 public class EventHub : Hub
{
    public void broadcastEvent(string userId = "",
                               string source = "",
                               string application = "",
                               string type = "",
                               string importance = "",
                               string message = "",
                               string timeStamp = "",
                               string stackTrace = "",
                               string exceptionMessage = "",
                               string innerExceptionMessage = "",
                               string objectContext = "",
                               string serverName = "",
                               string actionResult = "")
    {
        Clients.All.broadcastEvent(userId, source, application, type, importance, message, timeStamp, stackTrace, exceptionMessage, innerExceptionMessage, objectContext, serverName);
    }
}

and I have this Code in my web page:

 $(function () {
    var app = $.connection.eventHub;
    app.client.broadcastEvent = function (userId,
                                source,
                                application, 
                                type,
                                importance,
                                message,
                                timeStamp,
                                stackTrace,
                                exceptionMessage,
                                innerExceptionMessage,
                                objectContext,
                                serverName,
                                actionResult) {
        // Html encode display name and message. 
        var encodedName = $('<div />').text(name).html();
        var encodedMsg = $('<div />').text(message).html();
        // Add the message to the page.
        $('.row').append("div>asfdasdf</div>");
    };

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

And I also added the MapHub in the Startup of my project. My Question is:

On the server side I want to update the webpage as soon as new event has been created. How can I trigger the broadcast method on demand ? I can't just create an instance of the hub and call the method.

Pouyan
  • 2,849
  • 8
  • 33
  • 39
  • 1
    Can you use this: `var context = GlobalHost.ConnectionManager.GetHubContext(); context.Clients.All.broadcastEvent(....)`? As a side note - you should consider creating a class containing properties for the values you want to send instead of creating a function with 13 parameters. – Pawel Oct 20 '16 at 22:43
  • Thanks Pawel, it works like a miracle ! .. please put your comment as an answer. BTW, the reason behind all of those parameters is that I was not sure how to serialize my class for the client. Not sure how to pass it Can you give me more info on that too ? – Pouyan Oct 21 '16 at 15:59
  • Just create a class and pass an instance. It will be turned into a JSon and you will get an object on the other side. – Pawel Oct 21 '16 at 16:39

1 Answers1

1

You can access hub context via GlobalHost for instance:

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.broadcastEvent(...)
Pawel
  • 31,342
  • 4
  • 73
  • 104
  • Is it possible to stream data from SQL Server database through SignalR whenever any insertion or modification done in the tables? I need to show real time updation from Database. As of now, I am using setInterval method to poll data from Database. – RGS Dec 21 '16 at 17:46
  • 1
    I guess you use SqlDependency to be notified about changes to the db (http://stackoverflow.com/questions/15225147/watch-for-a-table-new-records-in-sql-database) and then use the above to invoke a hub method. – Pawel Dec 21 '16 at 18:02
  • Is it good practice enabling service broker in SQL Server? is there any performance degratation will occur in SQL server? – RGS Dec 21 '16 at 18:06
  • Well, the service is there. It is hard to comment if this is a good practice. If you need it you enable it if you don't need it you don't enable it. The good practice probably is to not enable it if you don't need it. – Pawel Dec 21 '16 at 18:09
  • Is there any alternative way to implement real time updation from SQL Server through SignalR without enabling the service broker? – RGS Dec 21 '16 at 18:23
  • 1
    I don't think SignalR is the key here. The key here is to get information about changes from SqlServer. You can either be notified by Sql Server using SqlDependency or poll for changes from the other side. Once you know something has changed you can do whatever you want (for instance use SignalR to notify clients). If you want other ways of being notified by SqlServer you could try using triggers and then maybe a CLR stored procedure but my feeling is it would be even more troublesome. – Pawel Dec 21 '16 at 18:49