1

I have read some SignalR tutorials. In order to implement SignalR in my existing Asp.Net solution application I did following:

I have created a separate MVC 4 project and created a Hub class along with some methods, so that it can be called from any other MVC project/Client present in the existing solution, means I would just like to use it as a service.

But now I have two questions:

  1. How can I call it from some other MVC project. Will it be the same way as mentioned in the tutorials, example like adding necessary script files in head of my .cshtml page and using js script like following:

    var hub = $.connection.; //and then

    hub.server.send("some args");

  2. I want to call Hub code also directly from the Server side code. Did it before when I was using SuperSocket. Would like to do same approach using SignalR. How can I do it ?

If you answer, then kindly please give some code sample(s). Many Thanks.

Faisal Mq
  • 5,036
  • 4
  • 35
  • 39

1 Answers1

3

There is sample code for calling client methods from outside the Hub class (but within the application) here. Basically, you use the ConnectionManager to call a hub's clients.

var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.myClientMethod(myValue);

To call this from outside your application, I'd use some sort of exposed method or service and wrap the above code.

michaelrp
  • 663
  • 4
  • 9
  • var context = GlobalHost.ConnectionManager.GetHubContext(); Will it be possible to call this from some other project, other than the project in which you actually implemented Hub class. I did this in SuperSockets. Want to know if it can be done similar way through SignalR. Actually in some scenarios want to send message from some other (non-hub) project directly to some connected client of hub. – Faisal Mq Jul 24 '13 at 04:49
  • Sorry just saw your provided link. Excellent answer. Thanks. – Faisal Mq Jul 24 '13 at 04:57
  • Is there any way to call my actual Hub method? This way of doing it seems to bypass MyHub and just go straight to the Hub Context, so any other code I have in the corresponding method on "MyHub" won't run...? Maybe I'm just missing something here. – mutex Nov 07 '13 at 22:43
  • I'm in the same boat. I'd like to call a method in my Hub class from another class. In my case, in the same project. My architecture consists of a web project and a service project. When a certain activity occurs inside my service, I want to have the Hub do it's job and UpdateThings() which will grab some data from the service and do some Clients.All work. Every solution I've seen so far only allows me access to the IHubContext which doesn't have my classes methods. – Brandon Wittwer Aug 11 '14 at 14:41
  • help me understand this ,i also have this problem. using web api 2 project for services and mvc 5 project as client app,my hub is at web api now i want to call a hub method from client application. #1. i am not able to generate automatic proxy probably because of separate projects. #2. if i use without generated proxy method ,server method dosent gets called , i get error at console log that ,error calling server method. – BR BHARDWAJ Dec 12 '15 at 12:54